JavaScript LastIndexOf method
The LastIndexOf method computes the last occurrence of the specified string in the entire string and returns the value. The syntax is as follows:
Copy Code code as follows:
Str_object.lastindexof (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 then starting from the last character of the string |
Tip: The string is counted starting from 0.
LastIndexOf Method Instance
Copy Code code as follows:
<script language= "JavaScript" >
var str = "Www.jb51.net";
document.write (Str.lastindexof (".") + "<br/>");
document.write (Str.lastindexof (".", 5) + "<br/>");
document.write (Str.lastindexof (".", 10));
</script>
Run the example and output:
Copy Code code as follows:
Note that in the example above, the second parameter is 5, the first one is actually found. Symbol, that is, searching in the www.5 string.
LastIndexOf is case sensitive, and if the string value to retrieve does not appear, the method returns-1.