String.IndexOf method(Char, [startIndex], [count])
Reports the index of the first occurrence of the specified character in this instance. The search starts at the specified character position and checks the specified number of character positions.
parameter
value
The Unicode character to find. The search for value is case-sensitive.
startIndex(Int32)
Optional, search starting position. If not set, it starts from 0.
count(Int32)
Optional, the number of character positions to check.
return value
The index position of value if the character is found; otherwise -1 if not found.
IndexOf()
Find the first occurrence of a specified character or string in a string and return the first index value, such as:
str1.IndexOf("word"); //Find the index value (position) of "word" in str1
str1.IndexOf("string"); //Find the index value (position) of the first character of "string" in str1
str1.IndexOf("Word",start,end);//Start from the start+1 character of str1, find the end characters, and find the position of "word" in the string STR1 [counting from the first character] Note: start+end cannot be greater than the length of str1
The indexof parameter is string, which searches for the first occurrence of the parameter string in the string and returns that position. For example, string s="0123dfdfdf"; int i=s.indexof("df"); then i==4.
If you need more powerful string parsing capabilities, 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 //Locate the first occurrence of d from front to back
test.indexof('d',1) =2 //Position d from front to back from the first occurrence of the third string
test.indexof('d',5,2) =6 //Position d from front to back, starting from the 5th position and checking 2 digits, that is, from the 5th to the 7th position;
lastindexof(): Position characters and strings from back to front in the string;,
The usage is exactly the same as indexof().
The following introduces IndexOfAny ||lastindexofany
They accept character arrays as arguments, and other methods are the same as above, returning 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
lastindexofany Same as above.