We can use JavaScript to check if a string contains only certain characters by using nested for loops to check individually if each character is one of those characters or not.
function containsCertainCharacters(theString, chars){
for( var i=0; i<theString.length; i++ ){
for( var k=0; k<chars.length; k++ ){
if(theString.charAt(i) === chars.charAt(k)){
return true;
}
}
}
return false;
};
console.log(containsCertainCharacters("Hello World!", "H"));
console.log(containsCertainCharacters("Hello World!", "olz"));
console.log(containsCertainCharacters("Hello World!", "z"));
#Output:
true
true
false
When working with strings, it can be useful to know if there are certain characters contained in a string variable.
In JavaScript, we can easily get if a string contains certain characters in a string by looping over each character of the string and seeing if it is one of the given characters or not.
Below is our function again which will check if a string has certain characters or not for you in a string using JavaScript.
function containsCertainCharacters(theString, chars){
for( var i=0; i<theString.length; i++ ){
for( var k=0; k<chars.length; k++ ){
if(theString.charAt(i) === chars.charAt(k)){
return true;
}
}
}
return false;
};
console.log(containsCertainCharacters("Hello World!", "h"));
console.log(containsCertainCharacters("Hello World!", "Wor"));
console.log(containsCertainCharacters("Hello World!", "y"));
#Output:
false
true
false
Checking if Certain Character Appears in a String Using JavaScript
The example above is useful if you want to check if ANY character in your string is in another string. We can also use JavaScript to check if EACH of the characters appear in a string.
To do this, we will loop over the character and if we find a character that is not in the string, we will return false. Otherwise, once we have checked all characters, we will return true.
Below is a JavaScript function that will check if a string has all particular characters.
function containsAllCharacters(theString, chars){
for( var i=0; i<chars.length; i++ ){
if((theString.includes(chars[i])) == false){
return false;
}
}
return true;
};
console.log(containsAllCharacters("Hello World!", "Hl"));
console.log(containsAllCharacters("This is a string with some words.", "SwDds"));
console.log(containsAllCharacters("What's up?", "Uu"));
console.log(containsAllCharacters("Brrrr", "Pr"));
#Output:
true
false
false
false
Checking if a Vowel Appears in a String Using JavaScript
When working with strings, it can be useful to know if there are any vowels contained in a string variable.
In JavaScript, we can easily check if a string contains vowels by looping over each character of the string and seeing if it is a vowel or not.
We can take the functions from above and modify them slightly to see if there are any vowels in a string.
Below is a JavaScript function that will check if a string has a vowel and return true if it does, and false if it doesn’t. We will use the indexOf() method to help with this.
function containsVowel(theString){
for( var i=0; i<theString.length; i++ ){
if("aeiou".indexOf(theString[i]) != -1) {
return true;
}
}
return false;
};
console.log(containsVowel("Hello World!"));
console.log(containsVowel("This is a string with some words."));
console.log(containsVowel("What's up?"));
console.log(containsVowel("Brrrr"));
#Output:
true
true
true
false
Hopefully this article has been useful for you to use JavaScript to check if a string contains only certain characters.
Leave a Reply