栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java统计字符串单词个数的方法解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java统计字符串单词个数的方法解析

在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。

不说废话了直接贴代码:

实现代码:

 
  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 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.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 iter = map.keySet().iterator(); 
   while(iter.hasNext()){ 
    String key = iter.next(); 
    Integer num = map.get(key); 
    System.out.println(key + "ntt" + 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);   }

运行结果:

后面还有一些没有全部截下来

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148199.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号