function indexOf(str, val){
let strLen = str.length,
valLen = val.length
for(let i = 0; i < strLen; i++){
let matchLen = i + valLen
let matchStr = str.slice(i, matchLen)
if(matchLen > strLen){
return -1
}
if(matchStr === val){
return i
}
}
return -1
}
手写indexOf
最新推荐文章于 2023-12-12 00:25:48 发布
本文介绍了一个用于在字符串中查找子串的 JavaScript 函数实现。该函数通过遍历目标字符串并比较子串来确定子串的位置。如果找到匹配项,则返回子串开始的位置;否则返回 -1。此方法适用于简单的字符串搜索场景。

462

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



