java统计字符串中单词个数_java统计字符串单词个数的⽅法解
析
在⼀些项⽬中可能需要对⼀段字符串中的单词进⾏统计,我在这⾥写了⼀个简单的demo,有需要的同学可以拿去看⼀下。
不说废话了直接贴代码:
实现代码:
/**
* 统计各个单词出现的次数
* @param text
*/
public static void findEnglishNum(String text){
//出所有的单词
String[] array = {".", " ", "?", "!"};
for (int i = 0; i < array.length; i++) {
text = place(array[i],",");
java replace方法}
String[] textArray = text.split(",");
//遍历 记录
Map map = new HashMap();
for (int i = 0; i < textArray.length; i++) {
String key = textArray[i];
//转为⼩写
String key_l = LowerCase();
if(!"".equals(key_l)){
Integer num = (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 = ();
Integer num = (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); }
运⾏结果:
后⾯还有⼀些没有全部截下来
以上就是本⽂的全部内容,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,同时也希望多多⽀持指引⽹!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论