字符串屬性
string str = "打工人";Console.WriteLine(str);char s = str[0];Console.WriteLine(s);
?字符串內置API(函數)
1. Concat??拼接字符串
string s1 = "打";string s2 = "工";string s3 = "人";string sth=string.Concat(s1, s2, s3);Console.WriteLine(sth);//打工人
2.Containts? 判斷字符是否包含
bool ch=sth.Contains("人");
Console.WriteLine(ch);//true
?3.CopyTo? 復制字符串
char[] chars = new char[10];sth.CopyTo(1,chars,0,2);Console.WriteLine(chars);//工人
4. char轉字符串
string charstr=new string(chars);
Console.WriteLine(charstr);//工人
5. ToUpper? 小寫轉大寫
string abc = "aBcD";
Console.WriteLine(abc.ToUpper());//ABCD
6. ToLower 大寫轉小寫
Console.WriteLine(abc.ToLower());//abcd
7. Replace? 替換關鍵字
string sth1 = "打工人打工魂";
Console.WriteLine(sth1.Replace("人","people"));//打工人people打工魂
Console.WriteLine(sth1.Replace("打工魂",""));//打工人
8. StartsWith? 是否以..開頭
string name = "立訊機器人";
Console.WriteLine(name.StartsWith("立"));//True
9. EndsWith? 以...結尾
Console.WriteLine(name.EndsWith("人"));//True
10.? Equals? 是否相等
Console.WriteLine(name.Equals("立訊機器人"));string c1 = "123";string c2 = "123";Console.WriteLine(string.Equals(c1,c2));//Trueobject obj1=new object();object obj2=new object();Console.WriteLine(object.Equals(obj1,obj2));//比較引用類型 False
11.?IndexOF 從前往后查詢 首次在源字符串中出現的索引位置
string num = "ABCc123c";Console.WriteLine(num.IndexOf('a'));//-1Console.WriteLine(num.IndexOf('A'));//0Console.WriteLine(num.IndexOf('c'));//3//12.StringComparison.OrdinalIgnoreCase() 忽略大小寫進行查詢Console.WriteLine(num.IndexOf("c",StringComparison.OrdinalIgnoreCase));//2Console.WriteLine(num.IndexOf("c",5));//從5的位置開始查詢 7
12.StringComparison.OrdinalIgnoreCase() 忽略大小寫進行查詢
Console.WriteLine(num.IndexOf("c",StringComparison.OrdinalIgnoreCase));//2
Console.WriteLine(num.IndexOf("c",5));//從5的位置開始查詢 ? 7
13.?LastIndexOf() ?從后向前查詢首次在源字符串中出現的索引值位置
Console.WriteLine(num.LastIndexOf("a"));//-1
14.?IndexOfAny() ?從前往后查詢首次出現的指定字符數組中任意一個
Console.WriteLine(num.IndexOfAny(new char[] {'a','A','b','c'}));//0
15.IsNullOrEmpty() ?判斷參數字符串是否為 "" null Empty
string num1 = "";
string num2 = null;
string num3=string.Empty;
Console.WriteLine(string.IsNullOrEmpty(num1));//True
Console.WriteLine(string.IsNullOrEmpty(num2));//True
Console.WriteLine(string.IsNullOrEmpty(num3));//True
16.?Insert() ?在指定的位置插入字符串 生成新的字符串
string ss = "abc";
Console.WriteLine(ss.Insert(1,"w"));//awbc
17. Join()? 拼接字符串
char[] sw1 = new char[] {'a','b','c'};
string [] sw2 = new string[] {"aa","bb","cc"};
Console.WriteLine(string.Join("+",sw1));//a+b+c
Console.WriteLine(string.Join("-",sw2));//aa-bb-cc
18. Remove()? 刪除
Console.WriteLine(name.Remove(4));//立訊機器Console.WriteLine(name.Remove(2,2));//立訊人
19.??Split() 將字符串分割成字符串數組
string sum = "張三,李四,王五,趙六";
Console.WriteLine(sum.Split(','));
string[] sss = sum.Split(',');
20.將字符串轉換為字符數組
char[]char1=sum.ToCharArray();for(int i=0;i<char1.Length;i++){Console.WriteLine(char1[i]);}
21. Substring()? 截取字符串
Console.WriteLine(sum.Substring(1,1));//三
?22. StringBuilder()? 創建字符串
StringBuilder sb = new StringBuilder();sb.Append("你好啊!");Console.WriteLine(sb);//你好啊!Console.WriteLine(sb.Length);//4Console.WriteLine(sb.Remove(1,2));//你!Console.WriteLine(sb.Replace("你","*"));//*!