IndexOf、LastIndexOf、Substring的用法(有些名字大小写写错了)

IndexOf、LastIndexOf、Substring的用法

今天遇到截取字符串的问题,在网上查了IndexOf、LastIndexOf、Substring这三种截取字符串的使用总结如下:

  String.IndexOf

String.IndexOf 方法 (Char, Int32, Int32)
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.IndexOf(value, startIndex, count)

参数
value:要查找的 Unicode 字符。 
startIndex:搜索起始位置。 
count:要检查的字符位置数。
返回值(Int32):
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。

String.LastIndexOf

String.LastIndexOf 方法
报告指定的 Unicode 字符或 String 在此实例中的最后一个匹配项的索引位置。

名称 说明  
String.LastIndexOf (Char) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。
String.LastIndexOf (String) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。
String.LastIndexOf (Char, Int32) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
String.LastIndexOf (String, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
String.LastIndexOf (String, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。一个参数指定要用于指定字符串的搜索类型。
String.LastIndexOf (Char, Int32, Int32) 报告指定的 Unicode 字符在此实例内的子字符串中的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.LastIndexOf (String, Int32, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.LastIndexOf (String, Int32, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。参数指定当前字符串中的起始搜索位置,以及要用于指定字符串的搜索类型。
String.LastIndexOf (String, Int32, Int32, StringComparison) 报告指定的 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.Substring

String.Substring 方法
从此实例检索子字符串。

名称 说明
String.Substring (Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始。
String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。

示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文”
Label1.Text = str.Substring(11,7);//返回 “国际通邓事文*”

Label1.Text = str.Substring(str.Length-3,3); // 返回邓事文,即截倒数3位字符

总结:

IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;
IndexOf是从左向右查,LastIndexOf是从右向左查,不管是IndexOf还是LastIndexOf,索引序列都是从左到右的(起始值是0)
Substring是字符串截取,返回值是一个截取后的字符串。

时间: 2024-08-29 03:29:18

IndexOf、LastIndexOf、Substring的用法(有些名字大小写写错了)的相关文章

js中substr,substring,indexOf,lastIndexOf等的用法

1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert(src.substr(7,3)); 弹出值为:off 2.substringsubstring(start,end)表示从start到end之间的字符串,包括start位置的字符但是不包括end位置的字符. var src="images/off_1.png";alert(src.subs

JAVA中字符串函数subString的用法小结

本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str; demo:

substring、sunstr、Substring的用法

一:在js中截取字符串的方法有两个:substring和sunstr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex到索引为endIndex之间的字符 substr(int stringIndex,[int Length]) 截取从索引为2开始后面的4个字符 2.注意事项: (1).这里的索引是从0开始的 (2).substring后的参数starIndex是必须填写的,tendIndex是可选参数 (3).su

String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别

本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在String类中,常用的定位子串和字符的方法包括IndexOf/LastIndexOf及IndexOfAny/LastIndexOfAny,下面进行详细介绍. 1.IndexOf/LastIndexOf IndexOf方法用于搜索在一个字符串中,某个特定的字符或者子串第一次出现的位置,该方法区分大小写

IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; namespace CA100 { class Program { //循环次数:5百万次 const int COUNT = 5000000; //外围循环次数:5次 const int NUM = 5; //准确测量运行时间 static

开发路程(4):C#中的SubString()的用法

先看语法: String.SubString(int index,int length)     index:开始位置,从0开始       length:你要取的子字符串的长度 例子: 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace str_sub 6 { 7 class Program 8 { 9 static void Main(string[] args) 10

js中substr,substring,indexOf,lastIndexOf,split等的用法

1.substr substr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert(src.substr(7,3)); 弹出值为:off 2.substring substring(start,end)表示从start到end之间的字符串,包括start位置的字符但是不包括end位置的字符. var src="images/off_1.png";alert(src.su

js中substr,substring,indexOf,lastIndexOf,split 的用法

1.substr substr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert(src.substr(7,3)); 弹出值为:off 2.substring substring(start,end)表示从start到end之间的字符串,包括start位置的字符但是不包括end位置的字符. var src="images/off_1.png";alert(src.su

IndexOf、LastIndexOf、Substring的用法

今天遇到截取字符串的问题,在网上查了IndexOf.LastIndexOf.Substring这三种截取字符串的使用总结如下:   String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.String.IndexOf(value, startIndex, count) 参数value:要查找的 Unicode 字符. startIndex:搜索起始位置.