java统计字符串单词的个数
来源:互联网 发布:淘宝已经收货申请退款 编辑:程序博客网 时间:2024/05/08 00:24
在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。
本人没怎么写个播客,如果有啥说的不对的地方,希望大家指正。
查询指定字符串出现次数可以直接使用:StringUtils.countMatches(str, sub)
统计所有单词出现次数代码:
/** * 统计各个单词出现的次数 * @param text */public static void findEnglishNum(String text){//找出所有的单词String[] array = {".", " ", "?", "!"};for (int i = 0; i < array.length; i++) {text = text.replace(array[i],",");}String[] textArray = text.split(",");//遍历 记录Map<String, Integer> map = new HashMap<String, Integer>();for (int i = 0; i < textArray.length; i++) {String key = textArray[i];//转为小写String key_l = key.toLowerCase();if(!"".equals(key_l)){Integer num = map.get(key_l);if(num == null || num == 0){map.put(key_l, 1);}else if(num > 0){map.put(key_l, num+1);}}}//输出到控制台System.out.println("各个单词出现的频率为:");Iterator<String> iter = map.keySet().iterator();while(iter.hasNext()){String key = iter.next();Integer num = map.get(key);System.out.println(key + "\n\t\t" + num + "次\n-------------------");}}
测试代码:
public static void main(String[] args) {String text = "Welcome welcome to ADempiere, a commons-based peer-production of Open Source ERP Applications. This Wiki is for the global community to contribute and share know-how and domain expertise. We hope you can find as much open information and participate in making it most usable for everyone. This project has a bazaar of Citizens with a Community Council Team which work in theFunctional Team and Technical Team along the Software Development Procedure supported and funded by the foundation ADempiere";findEnglishNum(text);}
运行结果:
后面还有一些没有全部截下来
本人代码功力不够深厚,如果代码中有什么不对或不好的地方,欢迎各位大神指点!
1 0
- java统计字符串单词的个数
- 【Java字符串高效率】统计单词统计个数
- Java测验 统计字符串中每个“单词”的个数
- 统计字符串中单词的个数
- 统计字符串中单词的个数
- 统计字符串中单词的个数
- Java统计单词个数
- 统计字符串中单词个数
- 统计字符串中的单词个数
- 统计字符串中的单词个数
- 【字符串操作】01.统计一个字符串中单词的个数
- [Java]对字符串中的每个单词个数进行统计
- Java统计字符串中每个字符(单词)个数
- 统计字符串中单词的个数(指针)
- C语言统计一个字符串中单词的个数
- C—输入一个字符串 统计其中单词的个数
- 第十六周项目2----统计字符串单词的个数
- 【codevs1040】【01NOIPTG】统计单词个数,字符串的划分DP
- 开发中最常用的GitHub上 优秀的 Android 开源项目整理(精品)
- PowerPoint2007将Word 文档作为新演示文稿打开
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
- 关于tmux和常见vim使用
- echart如何设置饼图的颜色以及柱状图颜色
- java统计字符串单词的个数
- Apache HttpCore 学习
- 你可能不再需要Underscore
- iOS中定时器NSTimer使用
- 北大教授饶毅500余字毕业典礼致全文
- 141. Linked List Cycle
- 第十一周【补充项目2输出职工信息】1
- Android 滑动分析(一)
- 第12周项目4-String类的构造