您的位置:首页 > 编程语言 > C#

C#字符串小知识

2015-12-28 10:51 204 查看
C#知识点很碎,下面介绍几个字符串常见函数的使用:

1.把字符串中所有的大写都变成小写

string B1 = "You are Beautiful!";
            string B2=B1 .ToLower ();
            Console.WriteLine(B2 );
            Console.ReadKey();



2.把字符串中所有的小写都变成大写

string B1 = "You are Beautiful!";
            string B2 = B1.ToUpper() ;
            Console.WriteLine(B2);
            Console.ReadKey();



3.去掉字符串两端的空格

string str1 = " I am beautiful! ";
            string str2 = str1.Trim();
            Console.WriteLine(str2);
            Console.ReadKey ();



4.返回一个字符串

string s = "beautiful";
            string s1 = s.Substring(1, 2), s2 = s.Substring(2);
            Console.WriteLine(s1 );
            Console.WriteLine(s2);
            Console.ReadKey();



SubString(开始位置,子串长度),其指定从原字符串的第几个字符开始返回子串,字串包含几个字符。

如:

s1 = s.Substring(1, 2)


指定从第二个字符开始返回(因为下标从零开始),截取子串长度为2,所以结果为s1=ea

SubString(开始位置),是从开始位置截取到原字符串的最后

如:

s2 = s.Substring(2)


所以s2=autiful

5.判断两个字符串(或对象)是否相等

string s = "beautiful", s1 = "Beautiful", s2 = "beautiful";
            Console.WriteLine(s.Equals (s1 ));
            Console.WriteLine(s.Equals (s2 ));
            Console.ReadKey();



6.用指定的字符串newValue替换字符串oldValue字符串,或使用字符newChar替换里面oldChar字符

Replace(string oldValue,string newValue)和Replace(char oldChar,char newChar)

如:

string s = "ha,I'm a beautiful girl";
            string s1 = s.Replace("ha","HA");
            string s2 = s.Replace('a','B');
            Console.WriteLine(s1);
            Console.WriteLine(s2 );
            Console.ReadKey();

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: