C#中String类的几个方法(IndexOf、LastIndexOf、Substring),
分享于 点击 40201 次 点评:170
C#中String类的几个方法(IndexOf、LastIndexOf、Substring),
String.IndexOfString.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。 String.IndexOf(value, startIndex, count) 参数 value:要查找的 Unicode 字符。 startIndex:搜索起始位置。 count:要检查的字符位置数。 返回值(Int32): 如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。 示例: string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文"; Label1.Text = str.IndexOf("中国").ToString();//返回 -1 Label1.Text = str.IndexOf("盈基").ToString();//返回 3 Label1.Text = str.IndexOf("盈基",10).ToString();//返回21 说明:这是从第10个字符开始查起。 Label1.Text = str.IndexOf("邓",15,10).ToString();//返回 -1 Label1.Text = str.IndexOf("邓",15,20).ToString();//返回 -32 说明:从第15个字符开始查找,要查找的范围是从第15个字符开始后20个字符,即从第15-35个字符中查找。 String.LastIndexOfString.LastIndexOf 方法报告指定的 Unicode 字符或 String 在此实例中的最后一个匹配项的索引位置。
示例: string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文"; Label1.Text = str.LastIndexOf("邓文").ToString();//返回-1 Label1.Text = str.LastIndexOf("邓").ToString();//返回32 Label1.Text = str.LastIndexOf("邓",8).ToString();//返回-1 Label1.Text = str.LastIndexOf("邓",20).ToString();//返回14 Label1.Text = str.LastIndexOf("邓",33).ToString();//返回32 说明:在指定的范围内查找字符,这个范围是上面的输入的参数,理解为,从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置。示例中,0-8中没有“邓”字,所以返回-1,0-20范围中,有一个“邓”字在索引14位置上,0-33范围中有两个“邓”字,因为LastIndexOf是返回最后一个匹配项索引位置,所以返32,而不是14。 String.SubstringString.Substring 方法从此实例检索子字符串。
示例: string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文"; Label1.Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文” Label1.Text = str.Substring(11,7);//返回 “国际通邓事文*” 总结一下:IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;IndexOf是从左向右查,LastIndexOf是从右向左查,不管是IndexOf还是LastIndexOf,索引序列都是从左到右的(起始值是0) Substring是字符串截取,返回值是一个截取后的字符串。 |
C#几个经常用到的字符串截取
一、
1、取字符串的前i个字符
(1)string str1=str.Substring(0,i);
(2)string str1=str.Remove(i,str.Length-i);
2、去掉字符串的前i个字符
string str1=str.Remove(0,i);
string str1=str.SubString(i);
3、从右边开始取i个字符:
string str1=str.SubString(str.Length-i);
string str1=str.Remove(0,str.Length-i);
4、从右边开始去掉i个字符:
string str1=str.Substring(0,str.Length-i);
string str1=str.Remove(str.Length-i,i);
5、
6 、如果字符串中有"abc"则替换成"ABC"
str=str.Replace("abc","ABC");
7、c#截取字符串最后一个字符的问题!!!!!!!!!!!!!!!!!!!!!
str1.Substring(str1.LastIndexOf(",")+1);
8、C# 截取字符串最后一个字符
k = k.Substring(k.Length-1, 1);
相关文章
- 暂无相关文章
用户点评