JavaScript indexOf method
The indexOf method is used to calculate the first occurrence of a specified string in the string and return the value. The syntax is as follows:
The code copy is as follows:
str_object.indexOf( search, start )
Parameter description:
| parameter | illustrate |
|---|---|
| str_object | String (object) to operate |
| Search | Required. String to retrieve |
| start | Optional. Specify the location to start the search. If this parameter is omitted, the search will start from the first character of the string. |
Tip: The string is counted from 0.
indexOf method example
The code copy is as follows:
<script language="JavaScript">
var str = "www.VeVB.COM";
document.write( str.indexOf( "." ) );
</script>
Run this example and output:
The code copy is as follows:
3
indexOf is case sensitive, and if the string value to be retrieved does not appear, the method returns -1.