myString.indexOf( searchString, [ position ] ) in ECMAScript 262
Find the offset of a substring within the string.
Arguments
name | type | description |
---|---|---|
searchString | String | The substring to search for wi |
position | Number | [optional] The position to start searchin |
- Return Type
- String
Description
If the search string exists within the calling string, the smallest index position in the calling string which is less than position
is returned. If the search string does not exist in the calling string (or if all instances where it appears begin at a point less than position
) then -1
is returned.
Note that the search/comparison is case-sensitive. To do a case-insensitive test, see RegExp.test()
var sentence="The quick brown fox jumped over the lazy dog.";
var loc = sentence.indexOf('the');
// ** loc is 32, because "The" at position 0 is not the same as "the" which was searched for
var oLocations = [];
loc = sentence.indexOf('o');
while (loc!=-1){
oLocations.push(loc);
loc = sentence.indexOf('o',loc+1);
}
// ** oLocations is [12,17,27,42]