indexOf() method
The String object in JavaScript provides an indexOf(searchValue, fromIndex) method for retrieving the position (subscript) of the first occurrence of a character or string in the source string.
Among them, searchValue is the character or string to be retrieved, which is case-sensitive; fromIndex is the position (subscript) to start the retrieval, and the reasonable range is 0 to the length of the source string -1.
'i like yanggb'.indexOf('y', 5); // 7
At the same time, fromIndex can be omitted (polymorphism), and by default fromIndex retrieves the character or string starting from the first digit (0) of the string.
'i like yanggb'.indexOf('y'); // 7
It should be noted that when searchValue is a string, it can actually be regarded as retrieving the position (subscript) of the first character of the string in the source string.
'i like yanggb'.indexOf('yanggb'); // 7
In addition, this method will only return the position (subscript) of the first matching character, and the following characters will not be dealt with.
'i like yanggb'.indexOf('i'); // 0
Finally, if no matching characters are found in the source string, the method returns -1.
'i like yanggb'.indexOf('renj'); // -1
lastIndexOf() method
The String object in JavaScript also provides a lastIndexOf(searchValue, fromIndex) for retrieving the position (subscript) of the last occurrence of a character or string in the source string.
'i like yanggb'.indexOf('i'); // 3
Likewise, if no matching characters are found in the source string, the method returns -1.
'i like yanggb'.indexOf('i', 5); // -1
The usage of this method is basically the same as the indexOf() method, but the function is different.
"Every tomorrow has a story that belongs to yesterday."
Reprinted in: https://www.cnblogs.com/yanggb/p/11563246.html