字符串截取
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
字符串轉(zhuǎn)換大小寫
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(子串) ;最后一次出現(xiàn)的位置
3 str.IndexOf("ab",0);
插入子串
1 str.Insert(插入位置,插入子串) ;
2 s.Insert(2,"ab");
移出子串
這里Remove我已經(jīng)和Substring一起說了。就不細說了
1 str.Remove(其實位置,移出數(shù));
2 s.Remove(3,2);
該文章在 2021/1/29 8:37:03 編輯過