我有一个方法:
function isSubString(word,array){}
给定一个单词数组和一个单词,此方法会尝试该单词是否是数组中其他单词的子字符串。
for(var i=0;i<array.length;i++){
if(JSON.stringify(array[i]).indexOf(JSON.stringify(word))>0){
return 1;
}
}
return 0;
例如array=['home','dog']
和word='ho'
;
Word ho
是 home
的子字符串,但这种方法是错误的。有人可以帮助我吗?
最佳答案
您不需要JSON.stringify()
,请使用String.prototype.indexOf()
The
indexOf()
method returns the index within the calling String object of the first occurrence of the specified value, starting the search atfromIndex
. Returns-1
if the value is not found.
if(array[i].indexOf(word) >-1 ){
关于javascript - 如何检查字符串是否是带有indexof的子字符串[JAVASCRIPT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34658163/