【js】怎么判断 字符串是否包含指定字符


在 JavaScript 中,可以使用  String.prototype.includes()  方法来判断一个字符串是否包含指定的字符。该方法执行区分大小写的检查,如果  searchString  在字符串中,则返回  true  ,否则返回false。

例如:

let string = 'The lazy fox jumped over the fence';
if (string.includes('lazy')) {    
    console.log("The string 'lazy' was found in the string");
}if (string.includes('Lazy')) {    
    console.log('The string "Lazy" was found in the string');
} else {    
    console.log('"Lazy" was not found because the case does not match');
}




评论