java 实现统计某段文字在内容中出现的次数
一个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.");
}
}
最新文章
- mybatis入门基础(八)-----查询缓存
- ASP.NET Misconfiguration: Debug Information
- C#与Swift异步操作的差异
- MySQL ibdata1文件迁移
- Sequence.js 实现带有视差滚动特效的图片滑块
- uC/OS-II汇编代码
- Myeclipse 10 破解说明
- ios tweak之binary not signed (use ldid -S)问题解决
- ubuntu apt-get
- Swift 中的getting和setter的使用
- QTP自传之web常用对象
- memcached在Windows下的安装
- oracle数据库recover和restore的区别
- SpringMVC的@ModelAttribute注解简单使用(用户修改信息)
- 数列[专杀Splay版]
- gradle入门(1-6)将Java项目从maven迁移到gradle
- Node.js(day2)
- MyBatis-Plus的简单使用
- [Android实例] Android Studio插件-自动根据布局生成Activity等代码1.4 (开源)(申明:来源于网络)
- epoll的水平触发和边缘触发,以及边缘触发为什么要使用非阻塞IO