|
| JAVASCRIPT lastIndexOf() METHOD
The lastIndexOf() method returns the position of the last occurrence of a specified search string or search character to look for in a string. The lastIndexOf() method is case sensitive.
The lastIndexOf() method takes the string or character to search for as a compulsory argument and an optional to start from argument (counted backwards). Always remember that the first position always starts at 0 (and not 1). The lastIndexOf() method returns a -1 if no occurrences are found.
The general format is:
string.lastIndexOf(tosearchfor, tostartfrom[optional]);
Example Without The Optional tostartfrom Argument:
<script type="text/javascript">
var string="The brown box";
document.write(string.lastIndexOf("b"));
</script>
The above will output:
Example With The Optional tostartfrom Argument:
<script type="text/javascript">
var string="The brown box";
document.write(string.lastIndexOf("r", 5));
</script>
The above will output:
Example With No Match:
<script type="text/javascript">
var string="The brown box";
document.write(string.lastIndexOf("f"));
</script>
The above will output:
See Also:
|
|