调用下面方法即可得到文章内指定字符串出现的次数:
/**
* 统计关键词
*
* @param keyword
* @param text
* @return
*/
public static Integer getStatisticsKeywordByModeTwo(String keyword,
String text) {
Pattern pattern = null;
int number = 0;
StringBuffer patternBuf = new StringBuffer();
patternBuf.append(keyword);
try {
pattern = Pattern.compile(patternBuf.toString());
} catch (Exception e) {
e.printStackTrace();
}
Matcher m = pattern.matcher(text);
while (m.find()) {
number++;
}
System.out.println("《" + keyword + "》出现次数:" + number);
return number;
}