arrays - How check if a string is substring with indexof [JAVASCRIPT] -
i have method:
function issubstring(word,array){}
this method, given array of words , word, tries if word substring of others words in array.
for(var i=0;i<array.length;i++){ if(json.stringify(array[i]).indexof(json.stringify(word))>0){ return 1; } } return 0;
for example array=['home','dog']
, word='ho'
;
word ho
sub string of home
method wrong. can me?
you don't need json.stringify()
, use string.prototype.indexof()
the
indexof()
method returns index within calling string object of first occurrence of specified value, starting search @fromindex
. returns-1
if value not found.
if(array[i].indexof(word) >-1 ){
Comments
Post a Comment