【问题标题】:How to get the total count of occurence of a word in a sentence如何获取句子中单词的出现总数
【发布时间】:2016-09-20 04:49:53
【问题描述】:

我试图找出一个句子中单词出现的总数。 我尝试了以下代码:

String str = "This is stackoverflow and you will find great solutions  here.stackoverflowstackoverflow is a large community of talented coders.It hepls you to find solutions for every complex problems.";

    String findStr = "hello World";     
    String[] split=findStr.split(" ");

    for(int i=0;i<split.length;i++){
        System.out.println(split[i]);
        String indexWord=split[i];
        int lastIndex = 0;
        int count = 0;      
        while(lastIndex != -1){

            lastIndex = str.indexOf(indexWord,lastIndex);
            System.out.println(lastIndex);

            if(lastIndex != -1){
                count ++;
                lastIndex += findStr.length();
            }

        }
        System.out.println("Count for word "+indexWord+" is : "+count);
    }

如果我传递“堆栈解决方案”之类的字符串,则该字符串应拆分为两个(空格拆分),并且需要找到句子中每个字符串的出现次数。如果我只传递一个单词,则计数是完美的.代码必须匹配包含搜索字符串的子字符串。 eg:-句子中“stack”出现了3次,但计数只有2次。

谢谢。

【问题讨论】:

  • lastIndex += indexWord.length();替换lastIndex += findStr.length();?
  • 太棒了。它现在工作正常。感谢您节省我的时间。
  • 我将添加一个答案,以便将此问题标记为已解决

标签: java


【解决方案1】:

当您在匹配后增加lastIndex 时,您的意思是将其增加匹配的长度(indexWord),而不是输入字串的长度(findStr)。换行就行了

lastIndex += findStr.length();

lastIndex += indexWord.length();

【讨论】:

    【解决方案2】:

    试试这个代码

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int lastIndex = 0;
    int count = 0;
    
    while(lastIndex != -1){
    
    lastIndex = str.indexOf(findStr,lastIndex);
    
     if(lastIndex != -1){
        count ++;
        lastIndex += findStr.length();
     }
    }
    System.out.println(count);
    

    【讨论】:

      【解决方案3】:

      您也可以使用地图。

      public static void main(String[] args) {
      
              String value = "This is simple sting with simple have two occurence";
      
              Map<String, Integer> map = new HashMap<>();
              for (String w : value.split(" ")) {
                  if (!w.equals("")) {
      
                      Integer n = map.get(w);
                      n = (n == null) ? 1 : ++n;
                      map.put(w, n);
                  }
              }
              System.out.println("map" + map);
          }
      

      【讨论】:

        【解决方案4】:

        是否有任何理由不使用现成的 API 解决方案。 这可以通过使用 apache commons-lang 中的 StringUtils 来实现,该方法具有 CountMatches 方法来计算一个字符串在另一个字符串中出现的次数。

        例如

        String input = "This is stackoverflow and you will find great solutions  here.stackoverflowstackoverflow is a large community of talented coders.It hepls you to find solutions for every complex problems.";
        String findStr = "stackoverflow is";
        for (String s : Arrays.asList(findStr.split(" "))) {
                 int occurance = StringUtils.countMatches(input, s);
                 System.out.println(occurance);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-11-06
          • 1970-01-01
          • 2019-07-09
          • 1970-01-01
          • 1970-01-01
          • 2012-01-06
          • 1970-01-01
          • 1970-01-01
          • 2010-09-07
          相关资源
          最近更新 更多