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();//转大写函数 |