[英]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.我的函数试图在不使用 indexOf 或正则表达式match
或任何标准 JS 方法的情况下检查字符串是否包含子字符串。
Please check this jsfiddle: https://jsfiddle.net/09x4Lpj2/请检查这个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
).此解决方案仅在两个字符串(例如applegate
和apple
)之间的索引相同applegate
。 But will not work if the indexes are not the same (ex. applegate
and gate
).但如果索引不相同(例如applegate
和gate
),则applegate
。 How do I manipulate the iterative values correctly so that the function returns true for both situations?如何正确操作迭代值,以便函数在两种情况下都返回 true?
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
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.这段代码有一个逻辑问题,只判断A的最后一个字符是否等于B的对应字符,可能下面的代码就是你想要的,加一行代码。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.