搜索短语,循环执行不正确

时间:2017-02-01 17:17:29

标签: javascript

这是上一个问题的更新版本,但它需要不同的答案。

我正在尝试搜索字符串中的短语。

var string = "blahmehfoobar hello random stuff here blahblah blegh coding stackover flow computersc ience";

var textToFind = ["blah", "random stuff", "stackover flow comput"];
var counter = 0;

for (var i = 0; i < textToFind.length; i++){
    var text = textToFind[i];
    console.log('finding: ' + text);
    for (var j = 0; j < string.length; j++){
        if (text.charAt(0) === string.charAt(j)){
            console.log(string.substring(j, text.length));
            if (text === string.substring(j, text.length)){
                counter++;
            }
        }
    }
}

console.log(counter);

计数器最后应为5。

我得到的输出很奇怪:

finding: blah
blah
mehfoo
mehfoobar hello random stuff here
mehfoobar hello random stuff here blah
mehfoobar hello random stuff here blahblah
finding: random stuff

r hello
r hello random stuff he
r hello random stuff here blahblah blegh coding stackove
r hello random stuff here blahblah blegh coding stackover flow compute
finding: stackover flow comput
andom
andom stuff here blahblah blegh coding
andom stuff here blahblah blegh coding stackover flow computer

我不明白为什么在打印第一个找到的短语后正在执行string.substring(j,text.length)。我试过调试这个无济于事。

预期产出:

finding: blah
blah
blah
blah
finding: random stuff
random stuff
finding: stackover flow comput
stackover flow comput

编辑:我不是在寻找替代解决方案。我只是想知道为什么循环出错以及为什么我没有得到预期的输出

5 个答案:

答案 0 :(得分:1)

这是你如何使用子串的问题。起始值和结束值都是基于零索引的,而不是开始和长度。这将有效:

mysqld

请注意,我不是使用text.length作为我的终点,而是使用j + text.length - 这可以确保我正确地设置我的端点。

答案 1 :(得分:1)

至于为什么你的代码错了?这是因为您使用的是substring而不是substr

substr:接受两个参数,一个index,它将是子字符串开始的锚点,一个amount(多少个字符)。

substring:接受两个参数(两个索引,并返回从第一个到最后一个的子串)。

答案 2 :(得分:0)

您可以迭代搜索数组并使用带有起始值的indexOf进行搜索。

&#13;
&#13;
var lotsOfText = "blahmehfoobar hello random stuff here blahblah blegh coding stackover flow computersc ience",
    textToFind = ["blah", "random stuff", "stackover flow comput"];
    counter = textToFind.reduce(function (count, phrase) {
        var pos = lotsOfText.indexOf(phrase);
        while (pos !== -1) {
            count++;
            pos = lotsOfText.indexOf(phrase, pos + 1);
        }
        return count;
    }, 0);

console.log(counter);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

回答您的问题,请参阅下面代码段中的评论:

var string = "blahmehfoobar hello random stuff here blahblah blegh coding stackover flow computersc ience";

var textToFind = ["blah", "random stuff", "stackover flow comput"];
var counter = 0;

for (var i = 0; i < textToFind.length; i++){
    var text = textToFind[i];
    console.log('finding: ' + text);
    for (var j = 0; j < string.length; j++){
        if (text.charAt(0) === string.charAt(j)){
            console.log(string.substring(j, text.length));
            
            // just replace `if (text === string.substring(j, text.length))`
            //  .......with `if (text === string.substring(j, j+text.length))`
            if (text === string.substring(j, j+text.length)){
                counter++;
            }
        }
    }
}

console.log(counter);

现在没有回答你的问题......;) 即使您不是在寻找替代解决方案,也可以使用更简单的方法来查找文本。

var string = "blahmehfoobar hello random stuff here blahblah blegh coding stackover flow computersc ience";
var textToFind = ["blah", "random stuff", "stackover flow comput"];
var counter = 0, matches = [];

textToFind.map(function(search){
  matches = matches.concat(string.match(new RegExp(search, 'ig')));
});
console.log(matches.length);

答案 4 :(得分:0)

Anudder one ...

var myString = "blahmehfoobar hello random stuff here blahblah blegh coding stackover flow computersc ience";
var textToFind = ["blah", "random stuff", "stackover flow comput", "meh", "foo" ];

let counter = 0;
textToFind.forEach ( ( v, i ) => {
  let sub = myString;
    while ( i !== -1 ) {
      i = sub.indexOf ( v );
      counter += ( i === -1 ? 0 : 1 );
      sub = sub.slice ( i + v.length );
    }
});

Sneaky ......通过在forEach中使用迭代val“i”,您不必在子循环中初始化它。

如果我得到了要求,这应该记录“7”(并且确实如此)。