Length:屬性,返回字符串中的字符個數。app
IndexOf-搜索函數 |
int String.IndexOf(string value); |
返回字符串中第一次出現子字符串的字符位置,從0開始,未找到子字符串返回-1。 |
int String.IndexOf(string value,int startIndex); |
同上,可是從字符串的startIndex位置開始搜索,一直到字符串末尾。 |
int String.IndexOf(string value,int startIndex,int count); |
同上,可是從字符串的指定位置開始,並向後搜索count個字符。 |
注意後兩個參數的取值要知足下面條件: 0<=startIndex 0<=Length-startIndex |
示例:string mystr="ABCABCABC"; |
表達式 |
返回值 |
mystr.IndexOf("a") |
-1 //大小寫敏感 |
mystr.IndexOf("A") |
0 |
mystr.IndexOf("A",0) |
0 //第一個字符的索引號爲0 |
mystr.IndexOf("A",1) |
3 |
mystr.IndexOf("A",1,2) |
-1 |
mystr.IndexOf("A",1,3) |
3 //總共搜索count個字符 |
LastIndexOf-反向搜索函數 |
返回字符串中子字符串的最後出現的字符位置,從後向前找。 |
0<=startIndex 0<=startIndex+1 |
Insert-插入函數 |
string Insert(int startIndex,string value); |
在字符串的指定索引位置插入一個字符串。 在返回的字符串中,value字符串的第一個字符的索引號爲startIndex。 |
Remove-刪除函數 |
string Remove(int startIndex,int count); |
從字符串的指定索引位置刪除指定數目的字符。 索引和計數必須引用該字符串內的位置,startIndex+count<=Length |
Replace-替換函數 |
string Replace(char oldChar,char newChar); |
string Replace(string oldValue,string newValue); |
Concat-鏈接函數 |
string Concat(參數); |
參數能夠是對象、對象數組、字符串、字符串數組,多個對象(字符串)間以逗號分隔 |
Join-串聯函數 |
string Join(string separator,string[] value); |
在字符串數組的每一個元素之間串聯指定的分隔符,從而產生單個串聯的字符串。 |
string Join(string separator,string[] value,int startIndex,int count); |
串聯部分數組元素,從第startIndex個元素開始,串聯count個元素。 |
Split-分割函數 |
string[] Split(params char[] separator); |
根據分割字符將字符串分割成子字符串,而後將結果做爲字符串數組返回。 |
string[] Split(char[] separator,int count); |
參數count指定返回的最大數組元素數,進行部分分割 |
示例:string mystr="1+2+3-4+5+6"; |
表達式 |
返回 |
mystr.Split('-'); |
{"1+2+3","4+5+6"} |
mystr.Split("+-".ToCharArray()); |
{"1","2","3","4","5","6"} |
mystr.Split(new char[]{'+','-'}); |
{"1","2","3","4","5","6"} |
mystr.Split("+-".ToCharArray(),4); |
{"1","2","3","4+5+6"} |
mystr.Split("+-".ToCharArray(),100); |
{"1","2","3","4","5","6"} |
注意上面分隔字符的幾種用法 部分分割時,最多返回count個元素。 |
ToCharArray-打散函數(哈哈,借用Flash中的術語了) |
char[] ToCharArray(); |
將字符串中的字符複製到字符數組。 |
char[] ToCharArray(int startIndex,int length); |
將字符串中的指定子字符串內的字符複製到字符數組。 |
示例:string mystr="Diffmaker"; |
表達式 |
返回 |
mystr.ToCharArray(); |
{'D','i','f','f','m','a','k','e','r'} |
mystr.ToCharArray(4,4); |
{'m','a','k','e'} |
Trim|TrimStart|TrimEnd-修剪函數 |
string Trim();//移除字符串首尾空白字符(包括中英文空格) |
string TrimStart();//移除字符串首部空白字符(包括中英文空格) |
string TrimEnd();//移除字符串尾部空白字符(包括中英文空格) |
string Trim(params char[] trimChars);//移除字符串首尾字符 |
string TrimStart(params char[] trimChars);//移除字符串首部字符 |
string TrimEnd(params char[] trimChars);//移除字符串尾部字符 |
當不指定參數時,移除的是空白字符 當指定參數時,移除的是指定字符 |
StartsWith|EndsWith-端點函數 |
bool StartsWith(string value);//檢測字符串是否以子串開始 |
bool EndsWith(string value);//檢測字符串是否以子串結束 |
PadLeft|PadRight-填充函數 |
string PadLeft(int totalWidth);//在字符串左側添加空格,使其達到指定長度 |
string PadRight(int totalWidth);//在字符串右側添加空格,使其達到指定長度 |
string PadLeft(int totalWidth,char paddingChar);//左側添加指定字符到定長 |
string PadRight(int totalWidth,char paddingChar);//右側添加指定字符到定長 |
Substring-取子函數 |
string Substring(int startIndex);//從指定的字符位置開始至串尾 |
string Substring(int startIndex,int length);//從指定的字符位置開始取指定長度 |
startIndex 從零開始 若是startIndex等於字符串的長度,則返回:string.Empty startIndex+count<=Length |
其餘簡單函數 |
String.ToLower();//轉小寫函數 |
String.ToUpper();//轉大寫函數 |