標籤:
參考型別 string 常用的
1.判斷是否包含字串
string a="我ABCDEFG愛你";
Contains("字串");
bool result=a.Contais("ABCDEFG")
返回:true;
2.分割符Split截取欄位
string b="abcdef&fghij&klmn";
string [] c=b.split("&");
c[0]="abcdef";
c[1]="fghij";
c[2]="klmn";
3.連接字串的方法。
3.1 Concat
string stra="Hello";
string strb="World!";
string newstr="";這裡順便提醒下自己string str=null;string str="";的區別。一句話,前者未分配記憶體,後者為str 開闢了一個""的記憶體空間。
newstr=Concat(stra,"",strb);
輸出值:Hello World!
3.2Join
string stra="Hello";
string strb="World!";
string newstr="";
string[] a={stra,strb};
newstr=string.join("",a);
輸出值:Hello word!
3.3 "+" 這個 不說了吧,因為想起來了 加上這個
4.有分割 有串連 坑定也插入 Insert(int InsertIndex,string value) ,索引是從0開始的
4.1 string stra="Hello";
string strb="World!";
string newstr="";
newstr=stra.Insert(1,strb);
輸出值:HWorld!ell
5.刪除 Remove() 從一個字串的制定位置開始,刪除指定數量的字元。
Remove(int stratIndex,int count )
string stra="Hello";
string newStr="";
newStr=stra.Remove(1,3);
輸出值:Ho
還想起一個,Trim();TrimStart();TrimEnd();去除Null 字元串的。
6.替換 Replace();
Replace(char oldchar,char newchar);
string stra="Hello";
string newstr="";
newstr=stra.Replace("ll","r");
輸出:Hero
7.跟換大小寫
ToUpper();
ToLower();
C# String