c#编程基础之字符串函数

c#常用的字符串函数

例一:

获取字符串的大小写函数

ToLower():得到字符串的小写形式

ToUpper():得到字符串的大写形式

注意:

字符串时不可变的,所以这些函数都不会直接改变字符串的内容,而是把修改后的字符串通过函数返回值的形式返回。

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "GOOD";
            s=s.ToLower();//s.ToLower():返回值为字符串的小写
            Console.WriteLine(s);/* 不是改变了字符串的内容,而是生成一个新的全部变为小写的字符串,然后用s指向这个新的字符串。*/
            Console.WriteLine(s.ToUpper());//s.ToUpper()返回值为字符串的大写。
            Console.ReadKey();
        }
    }
}

运行截图:

 例二:

字符串去两边的空白函数

Trim();

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "    GOOD   ";
            Console.WriteLine("去两边空白函数使用前:\n|{0}|",s);//调用函数前字符串两边有空白
            s=s.Trim();
            Console.WriteLine("去两边空白函数使用后:\n|{0}|", s);//调用函数后字符串两边无空白
            Console.ReadKey();
        }
    }
}

程序截图:

例三:

字符串忽略大小写比较的函数

"abc"=="ABC"

 bool string.Equals(string value,StringComparsion comparisonType)(+2重载)
 确定此字符串是否与指定的System.String对象具有相同的值。
参数指定区域性,大小写以及比较所用的排序规则。
 异常:
            system.NULLReferenceException
            system.ArgumentException

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            /* *********************************************
             * bool string.Equals(string value,StringComparsion comparisonType)(+2重载) 
             * 确定此字符串是否与指定的System.String对象具有相同的值。
             * 参数指定区域性,大小写以及比较所用的排序规则。
             * 异常:
             *       system.NULLReferenceException
             *       system.ArgumentException
             ************************************************       
        
             * StringComparsion枚举类型
             * bool b="abc"=="ABC";
             * Ignore:区分,Case:大小写
             *==是区分大小写的比较,Equals("ABC", StringComparison.OrdinalIgnoreCase)是忽略大小写的比较.
             */
           
            //判断"abc"=="ABC"返回值bool型为false,忽略大小函数调用后此bool型为true

            bool b = "abc".Equals("ABC", StringComparison.OrdinalIgnoreCase);
            Console.WriteLine(b);//由于调用了忽略大小写函数,所以bool型返回值结果为true
            Console.ReadKey();
        }
    }
}

 运行结果:

 例四:

字符串的分割函数:

string[] Split(params char[] separator):
将字符串按照指定的分隔符分割为字符串数组

将字符串按照指定的分隔符分割为字符串数组函数:

1.将aaa,bbb,ccc,dddfdsajf按照‘,’进行分隔

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "aaa,bbb,ccc,dddfdsajf";
           
            /* string[] Split(params char[] separator):
             * 将字符串按照指定的分隔符分割为字符串数组
             */

            string[] strs = s.Split(',');//将字符串s按照','进行分割成字符串数组。
            
            //循环打印字符串数组strs中的内容
            foreach(string item in strs)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

 程序截图:

 2.移除结果中的空白字符的分隔函数

string[] Split(charp[] separator,StringSplitOptions options)
将字符串按照指定的char分隔为字符串数组

(option取 RemoveEmptyEntrles的时候移除结果中的空白字符)

将aaa,bbb,,ccc,dddfdsajf进行分隔,如果用上面那个方法进行分隔则会出现空白字符:

为了避免以上情况,可以使用这个函数解决。

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "aaa,bbb,,ccc,dddfdsajf";
            
            /* string[] Split(charp[] separator,StringSplitOptions options)
             * 将字符串按照指定的char分隔为字符串数组
             * 
             * (option取 RemoveEmptyEntrles的时候移除结果中的空白字符)
             */
           
            string[] strs = s.Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries);
            
            //循环打印字符串数组strs中的内容
            foreach(string item in strs)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

程序运行结果:

 3.字符串作为分隔符进行分隔字符串函数:

string[] Split(string[] separator,StringSplitOptions options)
将字符串按照指定的string分隔符分割为字符串数组。

如:将"我是星云我是fairy我是颜可"分隔成"星云fairy颜可"

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "我是星云我是fairy我是颜可";
            
            /* string[] Split(new string[] separator,StringSplitOptions options)
             * 将字符串按照指定的char分隔为字符串数组
             * 
             * (option取 RemoveEmptyEntrles的时候移除结果中的空白字符)
             */
           
            string[] strs = s.Split(new string[] {"我是"},StringSplitOptions.RemoveEmptyEntries);
            
            //循环打印字符串数组strs中的内容
            foreach(string item in strs)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

程序运行截图:

练习一:

接受用户输入的一句英文,将其中的单词反序输出。
"hello c sharp"——>"sharp c hello"

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();//接受一个字符串
            string[] words = s.Split(' ');//将字符串进行分隔
            for (int i = words.Length - 1; i >= 0; i--)//反序输出
            {
                Console.Write(words[i] + " ");
 
            }
                Console.ReadKey();
        }
    }
}

程序运行截图:

练习二:

所用函数:

 int string.IndexOf(char value)(+8重载)
报告指定Unicode字符在此字符串中的第一个匹配项的索引。

string string.Substring(int startIndex,int length)(+1重载)
从此实例检索字符串中子字符串从指定的字符位置开始,且指有指定的长度.

异常:
          System.ArgumentOutofRangException

目标:将xingyun2684@gmail.com进行分隔,分隔出xingyun2684和gmail.com

源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string email= Console.ReadLine();//接受一个Email字符串,如xingyun2684@gmail.com
            
            /* *********************************************
             * int string.IndexOf(char value)(+8重载)
             * 报告指定Unicode字符在此字符串中的第一个匹配项的索引。
             *************************************************
             */
           
            int atIndex = email.IndexOf('@');//取@所在的位置

            /************************************************
             * string string.Substring(int startIndex,int length)(+1重载)
             * 从此实例检索字符串中子字符串从指定的字符位置开始,且指有指定的长度.
             * 异常:
             *       System.ArgumentOutOfRangeException
             ***********************************************       
             */

            string username = email.Substring(0,atIndex);//获取从开始位置到@所在位置的前一个位置的字符串,即xingyun2684
            string 域名 = email.Substring(atIndex + 1);//获取@所在位置+1的字符串,即gmail.com

            Console.WriteLine(username);//打印xignyun2684
            Console.WriteLine(域名);//打印gmail.com
                Console.ReadKey();
        }
    }
}

程序截图:

 练习三:

标题作者显示,直间。。。分隔

 源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = System.IO.File.ReadAllLines(@"d:\2.txt", Encoding.Default);
            foreach (string line in lines) 
            {
                string[] strs=line.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
                string title = strs[0];//标题
                string author = strs[1];//作者
                
                /* Math.Min(a, title.b);
                 * 返回a和b两个数的最小值。
                 */
             
                title = title.Substring(0, Math.Min(18, title.Length));
                title = title + "...";
                Console.WriteLine("{0}{1}", title, author);
            }
                Console.ReadKey();
        }
    }
}

 运行截图:

练习四:

 读取ini配置文件内容,函数传参数实现查询项目名对象的值。

 源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace 字符串函数学习
{
    class Program
    {
        static void Main(string[] args)
        {
            string value = GetConfigValue(@"d:\3.ini", "ip");//函数调用传参数,文件路径和项目名
            Console.WriteLine(value);
            Console.ReadKey();

        }
        static string GetConfigValue(string filename, string itemName) 
        {
            string[] lines = System.IO.File.ReadAllLines(filename, Encoding.Default);//读取一个文件
            foreach (string line in lines) //一行一行读
            {
                string[] strs = line.Split('=');//将字符串按照=进行分隔
                string name = strs[0];//ip
                string value = strs[1];//192.168.1
                if (name.Trim() == itemName)//判断查询是否相同 
                {
                    return value.Trim();
                }
            }
            return "没找到或文件路径不正确!";
        }
    }
}

 程序截图:

posted @ 2014-08-10 20:02  技术宅星云  阅读(4897)  评论(0编辑  收藏  举报