简体   繁体   中英

Check if string contains substring without using indexOf - Javascript

My function is trying to check if string contains substring without use of indexOf or regex match or any standard JS methods.

Please check this jsfiddle: https://jsfiddle.net/09x4Lpj2/

 var string1 = 'applegate'; var string2 = 'gate'; function containsString(string1, string2){ var j = 0; var k = 0; var contains = 'false'; var charArray1 = string1.split(''); var charArray2 = string2.split(''); for(var i = 0; i < charArray2.length; i++){ j = i; if(charArray1[j++] != charArray2[k++]){ contains = 'false'; }else{ contains = 'true'; } } console.log(contains); } containsString(string1, string2);

This solution works only when the indexes are the same between the two strings (ex. applegate and apple ). But will not work if the indexes are not the same (ex. applegate and gate ). How do I manipulate the iterative values correctly so that the function returns true for both situations?

you can try this modified script of yours.

 var string1 = 'applegate'; var string2 = 'gate'; var string3 = 'apple'; var string4 = 'leg'; var string5 = 'banana'; function containsString(string1, string2){ var charArray1 = string1.split(''); var charArray2 = string2.split(''); var match = 0; // iterate from start of 1st string until length of 1st string minus length of 2nd string // you don't need to iterate the last part that is not longer than 2nd string since it will be false for(var i = 0; i < charArray1.length - charArray2.length + 1; i++){ // reset match counter on every iteration match = 0; // iterate the 2nd string for(var j = 0; j < charArray2.length; j++){ // compare corresponding char location if(charArray1[i+j] == charArray2[j]){ match++; // just to check in console console.log(i, j, match, charArray1[i+j], charArray2[j]); } else { // just to check in console console.log(i, j, match, charArray1[i+j], charArray2[j]); // if not match, just skip current check break; } // if match already found, stop the checks, and return true if(match == charArray2.length){ return true; } } } // match not found until end of iteration return false; } console.log(containsString(string1, string2)); console.log(containsString(string1, string3)); console.log(containsString(string1, string4)); console.log(containsString(string1, string5)); // haystack does not contain needle console.log(containsString(string4, string1)); // haystack is shorter than needle

Welcome to SO. Regex can be used.. Unless even that is also prohibited..

 function containsString(string1, string2){
    console.log(string1.match(string2) != null ? "Yes" : "No");
 }

Regex

This code has a logical problem,Only to determine whether the last character of A is equal to the corresponding character of B,Maybe the following code is what you want,add a line of code.

var string1 = 'applegate';
var string2 = 'gate';

function containsString(string1, string2){
  var j = 0;
  var k = 0;
  var contains = 'false';
  var charArray1 = string1.split('');
  var charArray2 = string2.split('');

  for(var i = 0; i < charArray2.length; i++){
    j = i;
    if(charArray1[j++] != charArray2[k++]){
      contains = 'false';
      break;
    }else{
      contains = 'true';
    }   
  }

 console.log(contains);
}

Check this without using any inbuilt functions

function subSearch(long,short){
var count = 0
for(i = 0;i < long.length; i++){
  for(j = 0;j < short.length; j++){
    if(short[j] != long[i + j]){
      break;
    }
    if((j+1) == short.length){
      count++;
    }
  }
}
return count;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM