Search String for Substring Method
The Search String for Substring method searches the stringVar variable for the entire string that you specify in the substring argument. It returns the position of the first occurrence of this string.
If any of the following situations is true, then it returns a value of negative 1:
It does not find the value that you specify in the substring argument.
The value you specify in the offset argument is outside the range of positions in the string.
Values you enter for arguments are case-sensitive.
Format
stringVar.indexOf(substring [, offset])
The following table describes the arguments for the Search String for Substring method.
Argument | Description |
---|---|
substring |
One or more characters that this method searchs.The substring argument can contain a single character. |
offset |
The position in the string where this method starts searching. This method does the following according to how you set the offset argument:
|
Example 1
The following example returns the position of the first a character that occurs in the string. In this example, the first a character occurs at position 2:
var string = "what a string";
var firsta = string.indexOf("a")
The following example returns 3, which is the position of the first a character in the string when starting from the second character of the string:
var magicWord = "abracadabra";
var secondA = magicWord.indexOf("a", 1);
For more information, see the following topics: