字符串截取
SubString OR Remove
1 string str3 = "123abc456";
2
3 //str3 = str3.Substring(0, i); //从左边开始取字符串的前i个字符(str3 = str3.Remove(i, str3.Length - i);)
4 str3 = str3.Substring(0, 3);
5 str3 = str3.Remove(3, str3.Length - 3);
6 //输出:"123"
7
8 //str3 = str3.Substring(i); //从左边开始去掉字符串的前i个字符(str3=str3.Remove(0,i);)
9 str3 = str3.Substring(3);
10 //输出:"abc456"11
12 //str3 = str3.Substring(str3.Length - i); //从右边开始取i个字符(str3=str3.Remove(0,str3.Length-i))
13 str3 = str3.Substring(str3.Length - 3);
14 //输出:"456"
15
16 //str3 = str3.Substring(0, str3.Length - i); //从右边开始去掉i个字符(str3=str3.Remove(str3.Length-i,i))
17 str3 = str3.Substring(0, str3.Length - 3);
18 //输出:"123abc"
Trim
1 string str = " I Believe You Very Much! ";
2 str = str.TrimStart();
3
4
5 str = str.TrimStart('I');
6
7
8 str = str.TrimStart().TrimStart("Beli".ToCharArray());
9
10
11 string str1 = " I Believe You Very Much! ";
12 str = str1.Trim();
13
14
15
16 string str2 = " I Believe You Very Much! ";
17 str = str2.TrimEnd();
18
字符串替换
Replace
1 string str5 = "123abc456efg";
2 str5 = str5.Replace("efg", "EFG");
3
字符串分割
Split
1 string str6 = "123,abc,456,efg";
2 string[] str6s = str6.Split(',');
3 foreach (var item in str6s)
4 {
5 ConsoleStr(item);
6 }
7 //输出:"123 abc 789 efg"
字符串连接
Join
1
2 string str7 = string.Join("-", str6s);
3
字符串转换大小写
1 string str8 = "abcdefgHIJK";
2 str8 = str8.ToUpper();
3 //输出:"ABCDEFGHIJK"
4
5 str8 = str8.ToLower();
6 //输出:"abcdefghijk"
7
8 bool isUpper = char.IsUpper(str8, 8);
9 //输出:"True"
字符串比较
1 string str10 = "abackhdk";
2
3 string str9 = "abac";
4 int aa = str9.CompareTo(str10);
5
6
7 string str11 = "b";
8 int bb = str11.CompareTo(str10);
9
10
11 string str12 = "abackhdk";
12 int cc = str12.CompareTo(str10);
13
查找子串
1 str.IndexOf(子串,查找其实位置) ;
2 str.LastIndexOf(子串) ;最后一次出现的位置
3 str.IndexOf("ab",0);
插入子串
1 str.Insert(插入位置,插入子串) ;
2 s.Insert(2,"ab");
移出子串
这里Remove我已经和Substring一起说了。就不细说了
1 str.Remove(其实位置,移出数);
2 s.Remove(3,2);
该文章在 2021/1/29 8:37:03 编辑过