【问题标题】:How can I correctly check if a string does NOT contain a specific word?如何正确检查字符串是否不包含特定单词?
【发布时间】:2023-04-08 01:42:01
【问题描述】:

我目前正试图弄清楚如何解决上述问题。 具体来说,我想检查字符串是否 not 包含大写和小写字母的“stream”一词。

到目前为止,这是我的代码:

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

代码显然不起作用,因为结果不是我期望的那样。 在使用以下语法变体之前,我还尝试使用 indexOf 方法:

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

这些变化似乎都不适合我。谁能给我一个提示这里有什么问题?我之前多次使用indexOf 方法,但总是在我想检查字符串是否包含特定单词时,而不是相反。

【问题讨论】:

  • 在检查之前规范化大小写。 if (!name.toLowerCase().includes("stream")) {..
  • 你应该使用&amp;&amp;,而不是||。

标签: javascript string if-statement indexof contain


【解决方案1】:

感谢大家的快速反馈,代码现在运行良好!

我正在使用以下代码:

`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"}

`

【讨论】:

  • 如果您已经将另一个答案标记为答案,请不要发布您自己问题的答案。
【解决方案2】:

我更喜欢像这样使用 javascript RegExp:

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));

【讨论】:

    【解决方案3】:

    您可能希望将 include() 的返回结果与严格相等的操作数、=== false 或 === true 进行比较,这是更好的做法,但实际上并不需要这样做,看起来您可能会从了解中受益将布尔值与字符串进行比较是一件很奇怪的事情。我也不会检查“Stream”和“stream”,而是尝试使用 toLowerCase(),var str1_check = gewaesser_name1.toLowerCase();

    我会使用小写的“流”来检查流,因为您的新字符串都将是小写的,并且您希望它们与初始变量分开,因为您可能不希望这些名称被强制小写。我会使用 str1_check.includes("stream") 来检查这个字符串中是否有字符串“stream”,因为这个结果是真的还是假的,你可以像这样执行你的检查。

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

    我看起来像您的 if 逻辑,如果名字不包含“流”或名称 1 和 2 不包含流,但您的检查名称 1 带有小写“流”,名称 2 带有大写“流”。看起来您只是希望两个名称都不包含流,这样可以更容易地执行。

    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
    }
    

    【讨论】:

      【解决方案4】:

      indexOf() 是正确的方法,可以通过在测试前使用.toLowerCase() 或.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)));

      【讨论】:

        【解决方案5】:

        这是您可以在正则表达式中执行的操作:

        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) ))

        【讨论】:

          【解决方案6】:

          我建议使用String+toLowerCase 并检查String#indexOf,因为它适用于所有浏览器。

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

          【讨论】:

            猜你喜欢
            • 2011-05-20
            • 2013-12-23
            • 1970-01-01
            • 2011-07-20
            相关资源
            最近更新 更多