Hello Guys, Today In this tutorial we will learn How to Check if String Contains a word in Javascript? so let’s start this tutorial without wasting your time.
How to Check if String Contains a word in Javascript?
How to Check if String Contains a word in Javascript?
To Check if String Contains a word in Javascript The includes() method returns true if a string contains a specified string or word else it will return false. Here we have one string How to Check if String Contains a word in Javascript? and we want to check that our string variable contains a Javascript word or not. Then we need to use includes() and pass the string into it that you are looking for. string.includes(substring) will return true if the substring is exist and else it will return false. Nut remember String.includes method is case sensitive. Thank you.
Check if String Contains a word in Javascript
To Check if String Contains a word in Javascript You can also use the indexOf() method to check if strings contain a substring. Just call indexOf() with a parameter of substring just like this yourString.indexOf(yourSubString). indexOf() method Will return a positive number if the string contains a substring and the String is not containing a SubString then it will return -1. And If String does Not contain SubString then it will return -1. Hope, Now you understand How to Check if String Contains a word in Javascript? Thanks.
Method 1: Using includes()
To Check if String Contains a word in Javascript The includes() method returns true if a string contains a specified string or word else it will return false.
Here we have one string How to Check if String Contains a word in Javascript? and we want to check that our string variable contains a Javascript word or not. Then we need to use includes() and pass the string into it that you are looking for.
const string = 'How to Check if String Contains a word in Javascript?';
const substring = 'Javascript';
console.log(string.includes(substring));
Output Will Be.
True
string.includes(substring) will return true if the substring is exist and else it will return false. Nut remember String.includes method is case sensitive.
const string = 'How to Check if String Contains a word in Javascript?';
const substring = 'Javascript';
if (string.includes(substring)) {
console.log('String containing Javascript word ');
}else{
console.log('String not containing Javascript word ');
}
Thank you.
Method 2: Using indexOf() method
You can also use the indexOf() method to check if strings contain a substring. Just call indexOf() with a parameter of substring just like this yourString.indexOf(yourSubString).
indexOf() method Will return a positive number if the string contains a substring and the String is not containing a SubString then it will return -1. Just Like this.
const string = 'How to Check if String Contains a word in Javascript?';
const substring = 'Javascript';
const result = string.indexOf(substring);
console.log(result);
Output Will be.
42
And If String does Not contain SubString then it will return -1.
const string = 'How to Check if String Contains a word in Javascript?';
const substring = 'Javascripttttt';
const result = string.indexOf(substring);
console.log(result);
if (string.indexOf(substring) !== -1) {
console.log("Yes String Contain SubString");
}else{
console.log("No, String Contain SubString");
}
Hope, Now you understand How to Check if String Contains a word in Javascript? Thanks.
Conclusion
It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you.
Also, Read
- ModuleNotFoundError: No module named ‘requests’
- ValueError: I/O operation on closed file in Python
- An unhandled exception occurred: catch clause variable is not an Error instance
- fatal: detected dubious ownership in repository
- Composer detected issues in your platform: Your Composer dependencies require a PHP version