The usage of indexof in js is analyzed in detail
- 2020-03-30 01:01:48
- OfStack
String.indexof method (Char, [startIndex], [count])
Reports the index of the first match of the specified character in this instance. The search starts at the specified character position and checks for the specified number of character positions.
parameter
The value
The Unicode character to look for. The search for value is case sensitive.
StartIndex (Int32)
Optionally, search the starting position. If you don't, you start at 0.
Count (Int32)
Optionally, the number of character positions to check.
The return value
If the character is found, is the index position of value; Otherwise, if it is not found, it is negative 1.
IndexOf ()
Finds the first position of the specified character or string in the string, returns the first index value, such as:
Str1. IndexOf (" "); // find the index value (position) of the word in str1
Str1.IndexOf(" string "); // find the index value (position) of the first character of the string in str1
Str1. IndexOf (" word ", the start and end); // from the start+1 character of str1, find the end character, find the "word" in the string str1 position [from the first character] note :start+end cannot be greater than the length of str1
The indexof parameter is a string, find the position in the string where the parameter string first appears and return that position. Such as the string s = "0123 DFDFDF"; Int I = s.i ndexof (" df "); At this moment I = = 4.
If you need more powerful string parsing, you should use the Regex class, which USES regular expressions to match strings.
Indexof () : position characters and strings in a string from front to back; All return values are in the absolute position of the string, or -1 if null
The string test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
Test.indexof ('d') =2 // locate d first from front to back
Test.indexof ('d',1) =2 // locate d first from the third string from front to back
Test.indexof ('d',5,2) =6 // from front to back
Lastindexof () : position characters and strings in a string from the back forward; ,
The usage is exactly the same as indexof().
The following is an introduction to IndexOfAny ||lastindexofany
They accept an array of characters as arguments, and the other methods, as above, return the first subscript position of any character in the array
The following
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 ditto.