. Advertisement .
..3..
. Advertisement .
..4..
JavaScript is known as one of the core languages of programming that has been helping programmers all over the world to design websites. Due to the versatility of JavaScript, people are learning the concept to become professional programmer. Beginners try basic programs to get their hands on the programming front. To check if string contains specific character in JavaScript, you need to know that strings are the basic data that is used in JavaScript, and you can also witness it in many programs that you are trying.
As the string is an important part of JavaScript, you may want to check if the string contains a specific character in JavaScript. Let’s have a look at how to do it
How to check if string contains specific character in JavaScript?
There are a few methods that can help you find the specific characters held by a string.
Using the String.indexOf() Method
With the help of this method, you can easily check if a string contains a specific character. You will get in return the index of the first occurrence of substring specified inside the string object in return. If it doesn’t contain character, substring, or word, it returns -1. If it doesn’t return -1, then the string we are calling contains a character. Take a look at the code:
var sample_string = "Check If String Contains Specific Character In JavaScript";
var word = "JavaScript";
var character = "J";
var Substring = "How to";
// Check if the word exists in the given string or not.
if (sample_string.indexOf(word) !== -1) {
console.log('The word "' + word + '" exists in the given string.');
}
else {
console.log('The word "' + word + '" does not exists in the given string.')
}
// Check if the character exists in the given string or not.
if (sample_string.indexOf(character) !== -1) {
console.log('The character "' + character + '" exists in the given string.');
}
else {
console.log('The character "' + character + '" does not exists in the given string.');
}
// Check if the substring exists in the given string or not.
if (sample_string.indexOf(Substring) !== -1) {
console.log('The substring "' + Substring + '" exists in given string.');
}
else {
console.log('The substring "' + Substring + '" does not exists in the given string.');
}
Output:
The word "JavaScript" exists in the given string.
The character "J" exists in the given string.
The substring "How to" does not exists in the given string.
The operator (! ==) is an inequality operator that is also called (not equals to). You need to know that this method is case-sensitive.
Using the String.includes() Method
This is another method to check if the string contains character, substring, or word. With the String.includes()
Method, you will get True in return if the character or substring can be found, or else you will get False in return. Have a look at the example:
var sample_string = "Check If String Contains Specific Character In JavaScript";
var word1 = "Ittutoria";
var character1 = "n";
var substring1 = "In JavaScript";
// Check if the word exists in the given string or not.
if (sample_string.includes(word1)) {
console.log('The word "' + word1 + '" exists in the given string.');
}
else{
console.log('The word "' + word1 + '" does not exists in the given string.');
}
// Check if the character exists in the given string or not.
if (sample_string.includes(character1)) {
console.log('The character "' + character1 + '" exists in the given string.');
}
else{
console.log('The character "' + character1 + '" does not exists in the given string.');
}
// Check if the character exists in the given string or not.
if (sample_string.includes(substring1)) {
console.log('The substring "' + substring1 + '" exists in the the given string.');
}
else{
console.log('The substring "' + substring1 + '" does not exists in the the given string.');
}
Output:
The word "Ittutoria" does not exists in the given string.
The character "n" exists in the given string.
The substring "In JavaScript" exists in the the given string.
This method is case-sensitive. Internet Explorer doesn’t support this method, so if you don’t care about the browsers, then you can use this method.
Conclusion
And that’s how you check if string contains specific character in JavaScript. With the above methods, you can simply get through the code. I hope you find it useful!
Leave a comment