在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。
本人没怎么写个播客,如果有啥说的不对的地方,欢迎大家指正。
查询指定字符串出现次数可以直接使用:StringUtils.countMatches(str, sub)
统计所有单词出现次数代码:
/*** 统计各个单词出现的次数
* @paramtext
*/
public static voidfindEnglishNum(String text){
//找出所有的单词
String[] array = {".", " ", "?", "!"};
for (int i = 0; i < array.length; i++) {
text = text.replace(array[i],",");
}
String[] textArray = text.split(",");
//遍历 记录
Map map = new HashMap();
for (int i = 0; i < textArray.length; i++) {
String key =textArray[i];
//转为小写
String key_l =key.toLowerCase();
if(!"".equals(key_l)){
Integer num =map.ge