check a substring into a string

Sometimes we need to check the existence of a character or sub-string into a string or find the position of a character or sub-string into a string.

check a substring into a string

Javascript provides us a very simple method :
indexOf() :

 

searchvalue Required. The string to search for
start Optional. Default 0. At which position to start the search

Return type : number

The indexOf() method returns the position of the first occurrence of a specified value in a string.

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

 

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

See : lastIndexOf()

More Examples:

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

You Might Interested In

Leave a Reply

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