Definition and usage
The IndexOf () method returns the position of the first occurrence of a specified string value in a string.
Grammar
Stringobject.indexof (Searchvalue,fromindex)
Parameters
|
Describe |
Searchvalue |
Necessary. Specifies the string value that needs to be retrieved. |
Fromindex |
An optional integer parameter. Specifies the location to begin 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. |
Description
The IndexOf method returns an integer value, and the character position in Stringobject starts at 0 . If no substring is found, 1is returned.
Example:
<script type= "Text/javascript" >var str = "Hello world!"; Alert (Str.indexof ("World")); 6if (Str.indexof ("Hello")!=-1) {alert ("contains");} Else{alert ("not included");} </script>
JS determines whether a string contains a string IndexOf () method using