js是否包含某个字符


js是否包含某个字符

//es6 新增
str.includes("i")//如果字母存在返回 true 不存在 false
console.log(str.includes("i"))// true

str.startsWith("ai")//是否以选中的字符串开头 是 true 不是 false
console.log(str.startsWith("ai"))//false

1、indexOf()
var str = "123";
console.log(str.indexOf("3") != -1 ); // true
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有找到,则该方法返回 -12、includes()
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("world"); //true
includes() 方法用于判断字符串是否包含指定的子字符串,如果找到匹配的字符串则返回 true,否则返回 false。注意: includes() 方法区分大小写。

3、search()
var str="Visit W3School!"
console.log(str.search('W3School')) //6
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。如果匹配到字符串则返回,字符串所在索引。



0 0
讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
帮助