how to check if string contains substring in javascript

Frank
Frank
Member
26 Points
13 Posts

Hi,

how to check if string contains substring in javascript?

Views: 8948
Total Answered: 1
Total Marked As Answer: 1
Posted On: 26-Mar-2015 00:28

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Editor
1382 Points
14 Posts
         

Hi Frank,

Use javascript method indexOf() as follow.

<script type="text/javascript">
 
var str = "nice one code";
var substr = "nice";
var IsSubstringExists = false;
if (str.indexOf(substr) >= 0) {
IsSubstringExists = true;
} else {
IsSubstringExists = false;
}
alert(IsSubstringExists);
</script>

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: The indexOf() method is case sensitive.

 

Posted On: 25-Mar-2015 12:20
 Log In to Chat