Java 计算字符串中某个单词出现的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22566503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:32:20  来源:igfitidea点击:

Count the number of Occurrences of a Word in a String

javaregexstring

提问by Java Nerd

I am new to Java Strings the problem is that I want to count the Occurrences of a specific word in a String. Suppose that my String is:

我是 Java 字符串的新手,问题是我想计算字符串中特定单词的出现次数。假设我的字符串是:

i have a male cat. the color of male cat is Black

Now I dont want to split it as well so I want to search for a word that is "male cat". it occurs two times in my string!

现在我也不想拆分它,所以我想搜索“公猫”这个词。它在我的字符串中出现两次!

What I am trying is:

我正在尝试的是:

int c = 0;
for (int j = 0; j < text.length(); j++) {
    if (text.contains("male cat")) {
        c += 1;
    }
}

System.out.println("counter=" + c);

it gives me 46 counter value! So whats the solution?

它给了我 46 个计数器值!那么解决方案是什么?

采纳答案by Amit Joki

You can use the following code:

您可以使用以下代码:

String in = "i have a male cat. the color of male cat is Black";
int i = 0;
Pattern p = Pattern.compile("male cat");
Matcher m = p.matcher( in );
while (m.find()) {
    i++;
}
System.out.println(i); // Prints 2

Demo

演示

What it does?

它能做什么?

It matches "male cat".

它匹配"male cat"

while(m.find())

indicates, do whatever is given inside the loop while mfinds a match. And I'm incrementing the value of iby i++, so obviously, this gives number of male cata string has got.

指示,在m找到匹配项时执行循环内给出的任何操作。我正在增加iby的值i++,很明显,这给出了male cat一个字符串的数量。

回答by Jigar Joshi

Once you find the term you need to remove it from String under process so that it won't resolve the same again, use indexOf()and substring(), you don't need to do contains check length times

一旦找到该术语,您需要将其从正在处理的字符串中删除,以便它不会再次解决相同的问题,请使用indexOf()and substring(),您无需执行 contains 检查长度时间

回答by 3kings

The string contains that string all the time when looping through it. You don't want to ++ because what this is doing right now is just getting the length of the string if it contains " "male cat"

该字符串在循环时始终包含该字符串。你不想 ++ 因为它现在正在做的只是获取字符串的长度,如果它包含“”公猫”

You need to indexOf() / substring()

你需要 indexOf() / substring()

Kind of get what i am saying?

有点明白我在说什么?

回答by donfuxx

If you just want the count of "male cat"then I would just do it like this:

如果你只想要计数,"male cat"那么我会这样做:

String str = "i have a male cat. the color of male cat is Black";
int c = str.split("male cat").length - 1;
System.out.println(c);

and if you want to make sure that "female cat"is not matched then use \\bword boundaries in the split regex:

如果您想确保"female cat"不匹配,则\\b在拆分正则表达式中使用单词边界:

int c = str.split("\bmale cat\b").length - 1;

回答by Roberto

using indexOf...

使用 indexOf...

public static int count(String string, String substr) {
    int i;
    int last = 0;
    int count = 0;
    do {
        i = string.indexOf(substr, last);
        if (i != -1) count++;
        last = i+substr.length();
    } while(i != -1);
    return count;
}

public static void main (String[] args ){
    System.out.println(count("i have a male cat. the color of male cat is Black", "male cat"));
}

That will show: 2

这将显示:2

Another implementation for count(), in just 1 line:

count() 的另一个实现,仅在 1 行中:

public static int count(String string, String substr) {
    return (string.length() - string.replaceAll(substr, "").length()) / substr.length() ;
}

回答by AndreaTaroni86

If you find the String you are searching for, you can go on for the length of that string (if in case you search aa in aaaa you consider it 2 times).

如果您找到您正在搜索的字符串,您可以继续查找该字符串的长度(如果您在 aaaa 中搜索 aa,您会考虑它 2 次)。

int c=0;
String found="male cat";
 for(int j=0; j<text.length();j++){
     if(text.contains(found)){
         c+=1;
         j+=found.length()-1;
     }
 }
 System.out.println("counter="+c);

回答by Lucio

This staticmethod does returns the number of occurrences of a string on another string.

static方法确实返回一个字符串在另一个字符串上的出现次数。

/**
 * Returns the number of appearances that a string have on another string.
 * 
 * @param source    a string to use as source of the match
 * @param sentence  a string that is a substring of source
 * @return the number of occurrences of sentence on source 
 */
public static int numberOfOccurrences(String source, String sentence) {
    int occurrences = 0;

    if (source.contains(sentence)) {
        int withSentenceLength    = source.length();
        int withoutSentenceLength = source.replace(sentence, "").length();
        occurrences = (withSentenceLength - withoutSentenceLength) / sentence.length();
    }

    return occurrences;
}

Tests:

测试:

String source = "Hello World!";
numberOfOccurrences(source, "Hello World!");   // 1
numberOfOccurrences(source, "ello W");         // 1
numberOfOccurrences(source, "l");              // 3
numberOfOccurrences(source, "fun");            // 0
numberOfOccurrences(source, "Hello");          // 1


BTW, the method could be written in one line, awful, but it also works :)

顺便说一句,该方法可以写在一行中,很糟糕,但它也有效:)

public static int numberOfOccurrences(String source, String sentence) {
    return (source.contains(sentence)) ? (source.length() - source.replace(sentence, "").length()) / sentence.length() : 0;
}

回答by Lucio

This should be a faster non-regex solution.
(note - Not a Java programmer)

这应该是一个更快的非正则表达式解决方案。
(注意 - 不是 Java 程序员)

 String str = "i have a male cat. the color of male cat is Black";
 int found  = 0;
 int oldndx = 0;
 int newndx = 0;

 while ( (newndx=str.indexOf("male cat", oldndx)) > -1 )
 {
     found++;
     oldndx = newndx+8;
 }

回答by Karol Król

Java 8 version:

Java 8 版本:

    public static long countNumberOfOccurrencesOfWordInString(String msg, String target) {
    return Arrays.stream(msg.split("[ ,\.]")).filter(s -> s.equals(target)).count();
}

回答by zawhtut

Why not recursive ?

为什么不递归?

public class CatchTheMaleCat  {
    private static final String MALE_CAT = "male cat";
    static int count = 0;
    public static void main(String[] arg){
        wordCount("i have a male cat. the color of male cat is Black");
        System.out.println(count);
    }

    private static boolean wordCount(String str){
        if(str.contains(MALE_CAT)){
            count++;
            return wordCount(str.substring(str.indexOf(MALE_CAT)+MALE_CAT.length()));
        }
        else{
            return false;
        }
    }
}