【问题标题】:javascript,searching my namejavascript,搜索我的名字
【发布时间】:2017-07-26 10:21:21
【问题描述】:

我正在用 javascript 编写一个程序,它在文本中搜索我的名字并在控制台中记录相同的频率。 我首先检查文本的每个字母,当它与我名字的第一个字母匹配时,我使用另一个 for 循环将字母推入一个名为 hits 的数组中。字符串“text”中的字母被推到我的名字的长度使用 push ()。 在此之后,我检查数组“hits”和字符串“myName”是否相等,如果它们相等,我将计数加一。 但是我的代码不起作用,我不知道为什么,我想了很多,但一切都是徒劳的。请帮忙。

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
var count=0;
for(i=0;i<text.length;i++)
{
    if(text[i]===myName[0])
    {
        for(j=i;j<(i+myName.length);j++)
        {
            hits.push(text[j]);
        }
    }
    if(myName==hits)
    {
        hits=[];
        count=count+1;
    }
    hits=[];
}
if(count===0)
    console.log("Name not found!");
else
    console.log(count);
    
    

【问题讨论】:

  • 正则表达式呢?
  • if(myName==hits) 永远不会通过,因为您将字符串与数组进行比较。
  • 您是否有理由从头开始,而不是使用像 indexOf 这样的搜索功能?
  • 是的,我正在将字符串与数组进行比较,但我使用的是“==”而不是“===”
  • 谢谢 Teemu,你能推荐一些链接,我可以在其中阅读你所谈论的规则。感谢您的帮助。

标签: javascript arrays string


【解决方案1】:

您的代码失败,因为您将数组与字符串进行比较,这会给您带来错误,您可以使用 join() 从数组字符串中获取字符串,或者更好的方法是使用正则表达式,如下所示:

var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
var count=0;
console.log((text.match(new RegExp(myName, 'g'))).length);        
    

【讨论】:

    【解决方案2】:

    您正在将数组与字符串进行比较。在比较之前使用join()方法将其转换为字符串。

    例如

    if(myName==hits.join(''))
    

    工作代码:

    var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
    var count=0;
    for(i=0;i<text.length;i++)
    {
        if(text[i]===myName[0])
        {
            for(j=i;j<(i+myName.length);j++)
            {
                hits.push(text[j]);
            }
        }
        
        if(myName==hits.join(''))
        {
            hits=[];
            count=count+1;
        }
        hits=[];
    }
    if(count===0)
        console.log("Name not found!");
    else
        console.log(count);

    要更有效地获取出现次数,请查看How to count string occurrence in string?的答案

    【讨论】:

      【解决方案3】:

      使用Array#reduce 的简洁方法是:

      const text = 'History repeats itself. History repeats itself. Historians repeat each other.'
      
      function count(needle, haystack) {
        return haystack
          .split(' ')
          .reduce((c, e) => e === needle ? ++c : c, 0)
      }
      
      console.log(count('repeats', text))
      console.log(count('repeat', text))
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      编辑: 假设jsperf 表明此解决方案比正则表达式慢。

      【讨论】:

        【解决方案4】:

        你可以用正则来搜索你的名字,很简单:

        var name = "abhishek apolo bpple abhishek";
        var regexName = /abhishek/g;
        var matchname = name.match(regexName);
        if(matchname === null) {
            console.log("Name not found!");
        } else {
            console.log(matchname.length);
        }
        

        【讨论】:

          【解决方案5】:

          正则表达式呢?

          var text="abhishek apolo bpple abhishek",
            myName="abhishek",
            hits=[],
            regexp = new RegExp(myName,'g'),
            found ;
          
          // Thanks to https://stackoverflow.com/a/6323598/2846837
          do {
            found = regexp.exec(text) ;
            if ( found )
              hits.push(found[2]) ;
          } while ( found ) ;
          
          console.log(myName+' found : '+hits.length) ;

          【讨论】:

            【解决方案6】:

            只需使用 split() 即可。myName 是字符串 hits 是一个数组,在任何条件下都不相等

            var text = "abhishek apolo bpple abhishek";
            var myName = "abhishek";
            
            var count = text.split(myName).length - 1
            if (count === 0) {
              console.log("Name not found!");
            } else {
              console.log(count);
            }

            【讨论】:

              【解决方案7】:
              var text="abhishek apolo bpple abhishek",myName="abhishek",hits=[];
              
              var array_text =  text.split(" ");
              var count = 0
              for(var i in array_text){
                   if(myName == (array_text[i])){
                  count++;
                  hits.push(array_text[i]);
                 }
              }
              if(count===0)
                  console.log("Name not found!");
              else
                  console.log(count);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-04-13
                • 1970-01-01
                • 1970-01-01
                • 2023-03-23
                • 1970-01-01
                • 2023-04-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多