indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
如果没有找到匹配的字符串则返回 -1。
let str = "hello world , welcome."
console.log(str.indexOf("he")) // 0
console.log(str.indexOf("xxxx")) // -1
探索indexOf()方法:定位字符串在JavaScript中的位置
本文介绍了JavaScript的indexOf()方法,演示如何使用它查找指定字符串在字符串中的第一次出现位置,包括示例代码和未找到匹配时的返回值。
let str = "hello world , welcome."
console.log(str.indexOf("he")) // 0
console.log(str.indexOf("xxxx")) // -1

被折叠的 条评论
为什么被折叠?