JavaScript IndexOf method
The IndexOf method computes the first occurrence of a specified string in a string and returns that value. The syntax is as follows:
Copy Code code as follows:
Str_object.indexof (search, start)
Parameter description:
Parameters |
Description |
Str_object |
The string to manipulate (object) |
Search |
Necessary. The string to retrieve |
Start |
Optional. Specifies the location at which to start retrieving, such as omitting the argument, and starting from the first character of the string |
Tip: The string is counted starting from 0.
IndexOf Method Instance
Copy Code code as follows:
<script language= "JavaScript" >
var str = "Www.jb51.net";
document.write (Str.indexof ("."));
</script>
Run the example and output:
Copy Code code as follows:
IndexOf is case-sensitive, and if the string value to retrieve does not appear, the method returns-1.