js判断字符串是否包含某个字符串
方法一:includes方法
//str.includes("")返回一个布尔值,值为true时表示包含
var str = "hello world";
if(str.includes("world")){
alert("Hi,world");
}
方法二:indexOf方法
var str = "123456";
if(str.indexOf("5") != -1) {
console.log("字符串123456里包含了5");
} else {
console.log("字符串123456里没有包含5");
}
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。
完结!