How can I correctly check if a string does NOT contain a specific word?

124.3k views Asked by At

I am currently trying to figure out how to solve the above named problem. Specifically I want to check if the string does not contain the word "stream" both in capital and lowercase letters.

Here's my code so far:

if (((gewaesser_name1.includes("Stream") == "false") ||
(gewaesser_name1.includes("stream") == "false")) &&
((gewaesser_name2.includes("Stream") == "false") ||
(gewaesser_name2.includes("stream") == "false")))
{var a= "..."}

The code does obviously not work as the results are not what I expect them to be. I also tried to use the indexOf method before using the following syntax variations:

gewaesser_name2.indexOf("stream") == -1
gewaesser_name2.indexOf("stream") < 0

None of these variations seem to work for me. Could anyone please give me a hint what's the problem here? I used the indexOf method before many times but always when I wanted to check if a string did contain a specific word, not the other way round.

7

There are 7 answers

0
Nina Scholz On BEST ANSWER

I suggest to use String+toLowerCase and check with String#indexOf, because it works in every browser.

if (gewaesser_name1.toLowerCase().indexOf("stream") === -1 && gewaesser_name2.toLowerCase().indexOf("stream") === -1) {
    var a = "..."
}
0
Eiiki On

I would prefer to use javascript RegExp like this:

function includesMatch(lookupValue, testString){
    var re = new RegExp(lookupValue, 'i'); //Here the 'i' means that we are doing a case insensitive match
    return testString.match(re) !== null
}

var lookup = "stream";
var test1  = "do I have a StrEAm in me?";
var test2  = "well at least I don't";
console.log(includesMatch(lookup, test1));
console.log(includesMatch(lookup, test2));

1
niccober On

Thanks for the fast feedback guys, the code is now running perfectly fine!

I am using the following code:

`if      (gewaesser_name1.toLowerCase().indexOf("stream") === -1 && gewaesser_name2.toLowerCase().indexOf("stream") === -1) 
         {var a = "does NOT contain stream"}

 else    {var a= "does contain stream"}

`

0
Scott Marcus On

indexOf() is the correct approach and the case issue can be easily resolved by forcing the test string to lower or upper case before the test using .toLowerCase() or .toUpperCase():

const lookupValue = "stream";
const testString1 = "I might contain the word StReAm and it might be capitalized any way.";
const testString2 = "I might contain the word steam and it might be capitalized any way.";

function testString(testData, lookup){
  return testData.toLowerCase().indexOf(lookup) === -1;
}

function prettyPrint(yesNo){
   return "The string does" + (yesNo ? " NOT" : "") + " contain \"stream\" in some form."
}

console.log(prettyPrint(testString(testString1, lookupValue)));
console.log(prettyPrint(testString(testString2, lookupValue)));

0
maioman On

this is a operation you can do in regex:

const testString1 = "I might contain the word StReAm and it might be capitalized any way.";
const testString2 = "I might contain the word steam and it might be capitalized any way.";
const re = /stream/i
console.log( !!(testString1.match(re) ));
console.log( !!(testString2.match(re) ))

0
jgear91389 On

You may want to compare the returned results of your include() with strictly equal operands, === false or === true, it's much better practice however not really needed for this, just looks like you might benefit from knowing the different as comparing boolean to a string is an odd thing to do. I'd also not be checking "Stream" and "stream" try using toLowerCase() instead like so, var str1_check = gewaesser_name1.toLowerCase();

I'd check for stream using the lowercase "stream" as your new strings will all be in lower case, as well you want them to be separate from your initial variables as you may not want those names forced to lowercase. I'd use str1_check.includes("stream") to check if this string has the string "stream" in it, because this result is truthy or falsey you can perform your check like so.

if(str1_check.includes("stream")) {
//this string contained stream
}

I looks like your if logic here was if the first name doesn't contain "stream" or name 1 and 2 do not contain stream but your checking name 1 with lowercase "stream" and name 2 with uppercase "stream". it looks like you just want both names not to contain stream, this can be much more easily performed like this.

var str1_check = gewaesser_name1.toLowerCase(),
str2_check = gewaesser_name2.toLowrCase();//this way you're not making        multiple toLowerCase calls in your conditional and maintain the state of your two names.

if(!str1_check.includes("stream") && !str2_check.includes("stream")){
//your code on truthey statement
}
0
Lauri On

Here is something that could be an onliner with string to check as an argument.

const hasForbiddenFruit = diet => ['', '', 'apple'].some(diet.includes.bind(diet.toLowerCase()))
console.log(
   'Adam is good to go:', 
   hasForbiddenFruit(',  also some other aPpLe and so on')
)