0
我正在尝试编写一个搜索函数来搜索更长的字符串以查看它是否包含较小的字符串。我将这两个字符串作为参数传递给函数。这是我的代码:Javascript搜索函数
var search = function(wordToFind, stringToSearch) {
for (var a = 0; a<stringToSearch.length; a++) {
if(wordToFind.substring(0,wordToFind.length).toLowerCase()===stringToSearch.substring(a,a+wordToFind.length).toLowerCase()) {
return true;
} else {
return false;
}
}
}
当我通过它“你好”,它返回true,但是当我通过它任何其他组合的话,它返回false。有人可以帮助/解释为什么吗?
你可知道,字符串有,你可以使用'indexOf'方法? –
哦,'wordToFind.substring(0,wordToFind.length)'可以简写为'wordToFind' –
indexOf方法是什么?对不起,我对编码很陌生! – Max