Here we will discuss how we can check if a string contains a sub string or a particular text in JavaScript.
In one of our requirement we need to check if a string is presented inside a string or not using JavaScript.
Suppose I have a string like below in JavaScript.
var myString='Welcome to AspDotNetHelp.com world !!!';
Here we need to check if myString contains "AspDotNetHelp.com" or not.
In JavaScript we can use indexOf to check this. If indexOf returns value > 0 then the string is presented.
The code will be like below:
var myString='Welcome to AspDotNetHelp.com world !!!';
if( myString.indexOf('AspDotNetHelp.com') >= 0){
//string found
}
else
{
//string not found.
}
Hope this will be helpful.
In one of our requirement we need to check if a string is presented inside a string or not using JavaScript.
Suppose I have a string like below in JavaScript.
var myString='Welcome to AspDotNetHelp.com world !!!';
Here we need to check if myString contains "AspDotNetHelp.com" or not.
In JavaScript we can use indexOf to check this. If indexOf returns value > 0 then the string is presented.
The code will be like below:
var myString='Welcome to AspDotNetHelp.com world !!!';
if( myString.indexOf('AspDotNetHelp.com') >= 0){
//string found
}
else
{
//string not found.
}
Hope this will be helpful.