find last position of a substring into a string

Sometimes we need to find last position of a substring into a string. Substring may be a character or a part of a string. Javascript provide us a very simple method :
lastIndexOf() :

 

searchvalue Required. The string to search for
start Optional. The position where to start the search (searching backwards). If omitted, the default value is the length of the string

Return type : number

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

 

Note:
1. The lastIndexOf() method is case sensitive.
2. It returns position starting from 0 index.
3. It is supported in all major browsers.

See : indexOf()

More Examples:

Like this, we can find the position of a substring occurrence in the string.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.