黑马程序员————字符串的处理

来源:互联网 发布:胆码预测软件 编辑:程序博客网 时间:2024/05/16 08:32

---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------

 

字符串的处理:

字符串可以可以看做是char的只读数组。

例如:

            string str = "abcdefg";            char c = str[5];            Console.WriteLine(c);

1、将字符串转换成char数组  ToCharArray()

2、将char数组转换成字符串  new string(char[]);

例如:

string str = "abcdef";            //第一步 将字符串这个变量转换成char类型的数组 使用ToCharArray()这方法      char[] c = str.ToCharArray();            for (int i = 0; i < c.Length; i++)            {                c[0] = 'b';            }            //把char类型的数字 转换成字符串 用 newstring();            str = new string(c);            Console.WriteLine(str);

3、将字符串转换成小写  ToLower()

4、将字符串转换成大写   ToUpper()

5、比较两个字符串的时候,忽略大小写  S1.Equals(s2,.....);

例如:

//两个学员输入各自最喜欢的课程名称,判断是否一致,如果相等,则输出你们俩喜欢相同的课程.如果不相同,则输出你们俩喜欢不相同的课程.            Console.WriteLine("请输入你喜欢的课程");            string lessonOne = Console.ReadLine();            //   lessonOne = lessonOne.ToLower();//转换成小写            //    lessonOne = lessonOne.ToUpper();//转换成大写            Console.WriteLine("请输入你喜欢的课程");            string lessonTwo = Console.ReadLine();            //   lessonTwo = lessonTwo.ToLower();//转换成小写            //lessonTwo = lessonTwo.ToUpper();//转换成大写            if (lessonOne.Equals(lessonTwo, StringComparison.OrdinalIgnoreCase))            {                Console.WriteLine("你们喜欢的课程一样");            }            else            {                Console.WriteLine("你们喜欢的课程不一样");            }            Console.ReadKey();

6、SubString()截取字符串 

例如:

            string str = "abcedfg";            str = str.Substring(1, 3);

7、Index of   获取某一个字符的索引  找不到返回-1

例如:

            string str = "今啊天气天好晴朗,处处好风光";            int index = str.IndexOf('天', 3);

8、Last indexof 获取某一个字符最后一次出现的位置

例如:

            string str = @"E:\Music\杰伦MV\七里香";            int index = str.LastIndexOf('\\');

9、Remove  移除字符串

例如:

            string str = "今天天气好晴朗处处好风光";            str = str.Remove(1);

10、Contains 判断是否包含

例如:

            string str = "今天天气好晴朗,处处好风光";            bool b = str.Contains("今天");            if (b)            {                Console.WriteLine("包含");            }            else            {                Console.WriteLine("不包含");            }

11、StartsWith() 判断是否以.....开始

12、EndsWith() 判断是否以......结束

例如:

            string str = "今天天气好晴朗,处处好风光";            bool b = str.StartsWith("天");            bool b = str.EndsWith("光");

13、Replace()  替换

例如:

   string str = "笨蛋";            str = str.Replace("笨蛋", "**");


14、Split() 分割字符串,返回一个字符串的数组,后面那个枚举参数别忘了。StringSplitOption.RemoveEmptyEntries

例如:

//让用户输入一个日期格式如:2008-01-02,你输出你输入的日期为2008年1月2日            Console.WriteLine("请输入一个日期");            string input = Console.ReadLine();            string[] str = input.Split(new char[] { '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);            Console.WriteLine(str[0] + "年" + str[1] + "月" + str[2] + "日");            Console.ReadKey();


综合应用:

例1:

//接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"            string str = "abcdef";            char[] chs = str.ToCharArray();            for (int i = chs.Length - 1; i >= 0; i--)            {                Console.WriteLine(chs[i]);            }            Console.ReadKey();            char[] chs = str.ToCharArray();            for (int i = 0; i < chs.Length / 2; i++)            {                char temp = chs[i];                chs[i] = chs[chs.Length - 1 - i];                chs[chs.Length - 1 - i] = temp;            }            str = new string(chs);


例2:

 //接收用户输入的一句英文,将其中的单词以反序输出。"hello c sharp"→"sharp c hello"            string str = "hello c sharp";            string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);            for (int i = strNew.Length - 1; i >= 0; i--)            {                Console.Write(strNew[i] + " ");            }

例3:

//让用户输入一句话,找出所有e的位置            string str = "sdfsdfefdsfsdfefdsfsdfefdsfefdsfe";            int index = str.IndexOf('e');            int i = 1;            Console.WriteLine("第{0}次出现e的位置是{1}", i, index);            while (index != -1)            {                i++;                index = str.IndexOf('e', index + 1);                if (index == -1)                {                    break;                }                Console.WriteLine("第{0}次出现e的位置是{1}", i, index);            }



---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------