在一些项目中可能需要对一段字符串中的单词进行统计,我在这里写了一个简单的demo,有需要的同学可以拿去看一下。
不说废话了直接贴代码:
实现代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/**
* 统计各个单词出现的次数
* @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-------------------" );
}
}
|
测试代码:
?
1
2
3
|
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); }
|
运行结果:
后面还有一些没有全部截下来
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持快网idc!
原文链接:http://www.cnblogs.com/LFBlog/p/6240906.html
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Linux下进程管理工具Supervisor的安装配置和基本使用
2025-05-27 54 -
2025-06-04 55
-
2025-05-25 22
-
2025-05-25 35
-
2025-05-29 98
热门评论