java 字符串中出现的单词(字数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1936944/
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-10-29 18:36:31  来源:igfitidea点击:

Word occurrence in a String(word count)

javastringword-count

提问by landscape

Im stuck on writing Word occurrence in a string. I got some tip(in task notes) to use is compareToIgnoreCase. so I tried something like this:

我坚持在字符串中编写 Word 出现。我得到了一些提示(在任务说明中)使用的是 compareToIgnoreCase。所以我尝试了这样的事情:

splitwords = StringCont.split("\s");
for(int i=0; i<splitwords.length; i++)
{
    if(splitwords[1].compareToIgnoreCase(splitwords[i]) == 0)
        splitcount++;
}

It is of course just what I can do and probably bad way. When I run the code, I get sometimes out of array exeption and sometimes it runs. What is missing is: go through all words and check them and skip the words which were already counted. I will be happy to get any help with this so can move along and understand how it can be coded. Thank you :)

这当然是我能做的,而且可能是不好的方式。当我运行代码时,我有时会超出数组例外,有时它会运行。缺少的是:遍历所有单词并检查它们并跳过已经计算过的单词。我很乐意在这方面获得任何帮助,以便继续前进并了解如何对其进行编码。谢谢 :)

Edit: It seems I did not explain the problem enough clearly, but I get nice answer about the map object which easily put together what I needed. I did not know about map. So yea, I was trying to find the number of times every given word is found in the string.

编辑:似乎我没有足够清楚地解释这个问题,但我得到了关于地图对象的很好的答案,它很容易把我需要的东西放在一起。我不知道地图。所以是的,我试图找到在字符串中找到每个给定单词的次数。

tangens: it should mean-take the first word(where first whitespace is) splitwords[1] and compare it to all other words in string splitwords[i] and if it is 0(equals), then count++.

tangens:它应该意味着取第一个单词(第一个空格所在的位置)splitwords[1] 并将其与字符串 splitwords[i] 中的所有其他单词进行比较,如果它是 0(等于),则计数 ++。

Esko: there indeed are white spaces like in sentence. But I still got this exeption. I dont know why thru.

Esko:确实有句子中的空格。但我仍然得到了这个例外。我不知道为什么通过。

回答by BalusC

Store the words and their counts in a Map.

将单词及其计数存储在Map.

String[] words = string.toLowerCase().split("\s+");
Map<String, Integer> wordCounts = new HashMap<String, Integer>();

for (String word : words) {
    Integer count = wordCounts.get(word);
    if (count == null) {
        count = 0;
    }
    wordCounts.put(word, count + 1);
}

Note that I called toLowerCase()before split()as you seem want to have case insensitivity.

请注意,我toLowerCase()之前打过电话,split()因为您似乎希望不区分大小写。

回答by Esko

Are you looking for the occurrence of a certainword in sentence or the cumulated word count of the all the words in sentence? Based on your sample code I'd expect the former but your explanation makes me think of the latter.

您是在寻找句子中某个单词的出现次数还是句子中所有单词的累计词数?根据您的示例代码,我希望是前者,但您的解释让我想到了后者。

In any case, something obvious: If there aren't any whitespaces in the input String, String#split()returns an array containing exactly one element - the original String itself. Since array indexes start from zero, that splitwords[1]is causing the ArrayIndexOutOfBoundsExceptionbecause the only String available is in index [0].

在任何情况下,一些显而易见的事情:如果输入字符串中没有任何空格,则String#split()返回一个只包含一个元素的数组 - 原始字符串本身。由于数组索引从零开始,这splitwords[1]会导致 ,ArrayIndexOutOfBoundsException因为唯一可用的 String 在 index 中[0]

回答by Patrick

You can use a Set and store each word into and at the end get the size of the set.

您可以使用 Set 并将每个单词存储到其中,并在最后获得该集合的大小。

You will get the number of different wordnot the number of wordof course.

当然,您将获得不同单词数量,而不是单词数量

回答by tangens

If you want to count word occurence, you should use a HashMap<String, Integer>. There you can store a counter for each word found.

如果要计算单词出现次数,则应使用HashMap<String, Integer>. 您可以在那里为找到的每个单词存储一个计数器。