I write this is the basic knowledge of basic knowledge, why I still want to write, so-called good memory than rotten head, as a front-end developers, too many similar code see too much, but not common, so it is easy to forget, then I indexof the principle of clear
The IndexOf method is to return the retrieved string or the location of the character (or subscript, the index is retrieved from 0, and the starting bit of the retrieval starts from 0);
If a string is retrieved, it is the start of the subscript that returns the first character of the string;
Grammar
Stringobject.indexof (Searchvalue,fromindex)
Parameters |
Description |
Searchvalue |
Necessary. Specifies the string value that needs to be retrieved. |
Fromindex |
An optional integer parameter. Specifies where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, it is retrieved from the first character of the string. |
1 varstr = "This was this first"2 varCC = Str.indexof ("this");//=>03CC = Str.indexof ("This", "2")//=>84CC = Str.indexof ("T")//=>05CC = Str.indexof ("T", "2")//=>86CC = Str.indexof ("M")//= = If the string and character are not retrieved, return-17 Console.log (cc)8
Description
The method will retrieve the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to begin retrieving is at the fromindex of the string or at the beginning of the string (when no fromindex is specified). If a searchvalue is found, the position of the first occurrence of the Searchvalue is returned. The character position in Stringobject is starting at 0.
Hints and Notes
Note: the IndexOf () method is case-sensitive!
Note: If the string value that you want to retrieve does not appear, the method returns-1.
The IndexOf method of JS Stringobject