Java如何计算字符串中出现的单词?
package org.nhooo.example.commons.lang;
import org.apache.commons.lang3.StringUtils;
public class WordCountDemo {
public static void main(String[] args) {
//我们有要搜索的源文本。
String source = "From the download page, you can download the Java " +
"Tutorials for browsing offline. Or you can just download " +
"the example.";
//我们要计算其出现次数的单词
String word = "you";
//使用StringUtils.countMatches()方法,我们可以计算出现的次数
//给予者字符串中单词/字母的频率。
int wordFound = StringUtils.countMatches(source, word);
//打印我们找到多少个单词
System.out.println(wordFound + " occurrences of the word '" + word +
"' was found in the text.");
}
}这是上面代码的结果。
2 occurrences of the word 'you' was found in the text.
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬