java统计字符串单词个数的方法解析
2017-01-04 09:06
681 查看
在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。
不说废话了直接贴代码:
实现代码:
/** * 统计各个单词出现的次数 * @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); }
运行结果:
后面还有一些没有全部截下来
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
您可能感兴趣的文章:
相关文章推荐
- Qt字符串和java字符串的split方法比较和python的解析比较
- python统计文本字符串里单词出现频率的方法
- Java 统计一个字符串中每个单词,或者字母出现的次数
- Java统计字符串中子字符串个数的两种方法:
- Java统计字符串中每个字符(单词)个数
- 转:java写一个方法实现统计一条英文语句忠每个单词的个数
- java解析json字符串的两种方法详解(Android通用)
- Java 统计一个字符串中每个单词,或者字母出现的次数
- java统计字符串中重复字符出现次数的方法
- Java实现读取键盘输入保存到txt文件,再统计并输出每个单词出现次数的方法
- JAVA用标准库自己写一个字符串翻转方法,翻转字符串中字母非单词
- Java中统计字符串中大写、小写字母以及非英文字母数的三种方法
- php简单统计字符串单词数量的方法
- Java实现字符串解析为日期时间的方法示例
- 源码解读:java 解析字符串为boolean四种实现方法的细节
- 一没考虑单词重复,二没考虑先输入空格,三没考虑后面连续输空格,方法见空格将空格前面的当成一个字符串,字符串比较是否相同,再统计
- [Java]对字符串中的每个单词个数进行统计
- js eval方法解析java后台传来的json字符串
- Java简单统计字符串中汉字,英文字母及数字数量的方法
- java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法