c#string字符串

c#string字符串 //API 应用程序接口 内置函数//字符串的属性string a abcd;//表示字符串中 字符的个数Console.WriteLine(a.Length);//字符串是可以通过 索引 取值的 因为string类内部顶一个一个索引器char c a[2];Console.WriteLine(c);string s1 abc;string s2 吴亦凡;string s3 123;// string s4 s1 s2 s3;//1.Concat() 拼接字符串string s4 string.Concat(s1, s2, s3);Console.WriteLine(s4);//2.Contains() 判断参数字符串 是否出现在源字符串中//出现 返回true 不出现返回 falsebool b wuyifan.Contains(吴);Console.WriteLine();//声明一个长度为10的字符数组char[] chars new char[10];//3.CopyTo() 从字符串中复制一部分字符 放到一个字符数组中//参数1:被复制的字符串开始的索引//参数2:复制到的数组//参数3:从数组中第几个索引位置开始放//参数4:复制的个数s2.CopyTo(1, chars, 0, 2);//将char类型的数组 转换为字符串string charStr new string(chars);Console.WriteLine(charStr);string s5 A;//4.ToUpper() 将小写字母 转成大写字母Console.WriteLine(s5.ToUpper());//ABDDD//5.ToLower() 将大写字母 转成小写字母Console.WriteLine(s5.ToLower());//abdddstring str2 加拿大劣迹男艺人吴亦凡;//7.Replace() 替换字符Console.WriteLine(str2.Replace(吴, *));Console.WriteLine(str2.Replace(亦凡, **));Console.WriteLine(str2.Replace(吴亦凡, ***));//8.StartsWith() 判断源字符串 是否以参数字符串开头Console.WriteLine(str2.StartsWith(拿大));//9.EndsWith() 判断源字符串 是否以参数字符串结尾Console.WriteLine(str2.EndsWith(凡));//10.判断参数字符串和源字符串 是否完全相同 等同于 运算符Console.WriteLine(str2.Equals(加拿劣迹男艺人吴亦凡));int a1 10;int a2 10;Console.WriteLine(a1 a2);//比较的是值Console.WriteLine(a1.Equals(a2)); //比较的是值string ss1 abc;string ss2 abc;Console.WriteLine(ss1 ss2);Console.WriteLine(ss1.Equals(ss2));object obj1 new object();object obj2 new object();Console.WriteLine(obj1 obj2);//False//Equals() 在比较引用类型(string类型除外)的时候,比较的是内存地址Console.WriteLine(obj1.Equals(obj2));//False//索引 5//位置 6string s6 ABCscahihdifhu123434ccHIUDAHS;//11.IndexOf() 从前向后 查询参数字符串 首次 在源字符串中出现的索引位置,如果查询不到 返回-1Console.WriteLine(s6.IndexOf(e));Console.WriteLine(s6.IndexOf(Ac));//StringComparison.OrdinalIgnoreCase 忽略大小写进行查询Console.WriteLine(s6.IndexOf(c, StringComparison.OrdinalIgnoreCase));Console.WriteLine(s6.IndexOf(c, 5));//从索引5的位置开始查询//12. 从后向前查询参数字符串 首次 在源字符串中出现的索引位置,如果查询不到 返回-1Console.WriteLine(s6.LastIndexOf(A));//13.IndexOfAny() 从前向后查询源字符串首次在出现的指定字符数组中任意一个字符的位置Console.WriteLine(s6.IndexOfAny(new char[] { a, b, c }));//14.LastIndexOfAny() 从后向前查询源字符串首次在出现的指定字符数组中任意一个字符的位置Console.WriteLine(s6.LastIndexOfAny(new char[] { a, b, c }));string st1 ;string st2 null;string st3 string.Empty;string st4 132;//15.IsNullOrEmpty() 判断参数字符串 是否为 null EmptyConsole.WriteLine(string.IsNullOrEmpty(st1));Console.WriteLine(string.IsNullOrEmpty(st2));Console.WriteLine(string.IsNullOrEmpty(st3));Console.WriteLine(string.IsNullOrEmpty(st4));//16. Insert() 在指定的索引位置插入字符串 生成新的字符串Console.WriteLine(st4.Insert(0, 吴亦凡));char[] cs new char[] { a, c, d };string[] ss new string[] { aa, cc, dd };//17.Join() 将字符串数组或者字符数组,按照指定的字符拼接成一个字符串//参数1: 用什么字符拼接//参数2: 要拼接的数组Console.WriteLine(string.Join(,cs));Console.WriteLine(string.Join(-,ss));string n1 吴亦凡,罗志祥,李云迪;//18.Remove() 从索引为5的位置开始 删除后面所有的字符,返回删除后的字符串Console.WriteLine(n1.Remove(5));Console.WriteLine(n1.Remove(2,5));//从索引为2的位置开始,删除后面5个字符,返回删除后的字符串//19.Split() 使用指定的字符,将字符串分割成字符数组// string[] ss new string[] { 吴亦凡, 罗志祥, 李云迪 };Console.WriteLine(n1.Split(,));string[] chars2 n1.Split(,);for (int i 0; i chars2.Length; i){Console.WriteLine(chars2[i]);}//将数组转换为字符串Console.WriteLine(string.Join(,chars2));//19.ToCharArray() 将字符串转换为字符数组char[] chars3 n1.ToCharArray();for (int i 0; i chars3.Length; i){Console.WriteLine(chars3[i]);}//20.Substring() 截取字符串//参数1:开头的位置//参数2:截取的长度Console.WriteLine(n1.Substring(4,3));//string n2 吴亦凡 ;Console.WriteLine(n2);//21.Trim() 移除字符串前后的空格Console.WriteLine(n2.Trim());//TrimStart() 移除前面的空格Console.WriteLine(n2.TrimStart());//TrimEnd() 移除后面空格Console.WriteLine(n2.TrimEnd()1);