简体   繁体   English

如何查找字符串是否包含 switch() 中的内容 javascript

[英]how to find if a string includes something in a switch() javascript

i would like to find out if a string contains a certain word using a switch() statement我想使用switch()语句找出一个字符串是否包含某个单词

here is an example of what i want to use that for:这是我想用它来做的一个例子:

let text = "among"

switch(text.toLowerCase().includes()){
   case "among"
      console.log("not funny")
      break

   default:
      break
}

Switch statement evaluates an expression so this code will not work. Switch 语句对表达式求值,因此此代码将不起作用。 You could store possible inclusions in an array and loop through the array, then depending on the evaluation output a certain response with a switch statement.您可以将可能包含的内容存储在数组中并循环遍历数组,然后根据评估 output 使用 switch 语句做出特定响应。

let text = "among"

let words = ["among", "text", "test"]


function test(word, wordArr) {
    for (let w of wordArr) {
        if (w === word) {
            switch (w) {
                case "among":
                    console.log("among word");
                    break;
                default:
                    break;
            }
        }
    }
}

console.log(test(text, words))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM