java 实现统计某段文字在内容中出现的次数
http://outofmemory.cn/code-snippet/815/java-zishutongji
一个api,位于apache.commons.lang.StringUtils类下的一个StringUtils.countMatches(paragraph, string);方法,这个方法返回值是一个数字
一个api,位于apache.commons.lang.StringUtils类下的一个StringUtils.countMatches(paragraph, string);方法,这个方法返回值是一个数字
代码:
package cn.outofmemory;
import org.apache.commons.lang3.StringUtils;
public class WordCounter {
public static void main(String[] args) {
// String with our paragraph
String paragraph = "Java Code Geeks (JCGs) is an independent online community focused on creating the"
+ "ultimate Java-to-Java developers resource center; targeted at the technical architect, technical"
+ "team lead (senior developer), project manager and junior developers alike. JCGs serve the Java, "
+ "Scala, Android, SOA, Agile and Telecom communities with daily news written by domain experts, "
+ "articles, tutorials, reviews, announcements, code snippets and open source projects.";
// Declare the word you want to search
String string = "Java";
// Count word repetitions
int counter = StringUtils.countMatches(paragraph, string);
// Print the result
System.out.println("Word <" + string + "> appeared " + counter + " times in the paragraph.");
}
}
作者:SVENNEE
出处:http://www.cnblogs.com/svennee
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如果您觉得此文有帮助,请点击推荐和关注我。您的支持将鼓励我继续创作!
![](https://images.cnblogs.com/cnblogs_com/svennee/655802/o_by-nc-sa_2.5_88x31.png)
出处:http://www.cnblogs.com/svennee
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如果您觉得此文有帮助,请点击推荐和关注我。您的支持将鼓励我继续创作!
![](https://images.cnblogs.com/cnblogs_com/svennee/655802/o_by-nc-sa_2.5_88x31.png)