JavaScript lastIndexOf method
The lastIndexOf method calculates the last occurrence of the specified string in the entire string and returns the value. The syntax is as follows:
The code copy is as follows:
str_object.lastIndexOf( 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 last character of the string. |
Tip: The string is counted from 0.
lastIndexOf method example
The code copy is as follows:
<script language="JavaScript">
var str = "www.VeVB.COM";
document.write( str.lastIndexOf( "." ) + "<br />" );
document.write( str.lastIndexOf( ".", 5 ) + "<br />" );
document.write( str.lastIndexOf( ".", 10 ) );
</script>
Run this example and output:
The code copy is as follows:
9
3
9
Note that in the second example above with parameter 5, the first symbol is actually found, that is, searching in the www.5 string.
lastIndexOf is case sensitive, and if the string value to be retrieved does not appear, the method returns -1.