Home > Backend Development > C#.Net Tutorial > C# index of usage (reprint)

C# index of usage (reprint)

黄舟
Release: 2017-02-21 10:58:58
Original
1892 people have browsed it




Find the position where the specified character or string first appears in the string, return the first index value, such as :


str1.IndexOf("字"); //查找“字”在str1中的索引值(位置) 
str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置) 
str1.IndexOf("字",start,end);//从str1第start+1个字符起,查找end个字符,查找“字”在字符串STR1中的位置[从第一个字符算起]注意:start+end不能大于str1的长度
Copy after login

The indexof parameter is string, find the position where the parameter string first appears in the string and return that position. For example, string s="0123dfdfdf"; int i=s.indexof("df"); then i==4. Returns -1 if not found.

If you need a more powerful string parsing function, you should use the Regex class and use regular expressions to match strings.

indexof(): Position characters and strings from front to back in the string; all return values ​​refer to the absolute position in the string, if empty, it is - 1


string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
test.indexof(’d’) =2          //从前向后 定位 d 第一次出现的位置
test.indexof(’d’,5,2)=6         //从前向后 定位 d 从第5 位开始查,查2位,即 从第5位到第7位;
Copy after login


lastindexof(): Position characters and strings from back to front in the string;
Usage is exactly the same as indexof().

Introduced below IndexOfAny ||lastindexofany

They accept a character array as a variable, other methods are the same as above, and return the earliest subscript position of any character in the array

As follows :

 char[] bbv={’s’,’c’,’b’};
 string abc = "acsdfgdfgchacscdsad";
 Response.Write(abc.IndexOfAny(bbv))=1
 Response.Write(abc.IndexOfAny(bbv, 5))=9
 Response.Write(abc.IndexOfAny(bbv, 5, 3))=9
Copy after login

lastindexofany Same as above.

substring() Usage

string a="aadsfdjkfgklfdglfd"

a.substring(5) //Intercept all strings starting from the fifth digit

a.substring(0,5) //Intercept all strings from the 0th to the 5th.

The above is the content of C# index of usage (reprinted). For more related content, please pay attention to PHP Chinese Net (m.sbmmt.com)!


Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template