1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
java.util.*;
import
java.util.regex.*;
public
class
Yugi{
public
static
void
main(String[] args){
String words1=
"Look buddy, U got work hard and put yourself in your java, Once you learned the heart of the java, I can guarantee that you win."
;
String words=words1.toString().toLowerCase();//转换成小写
String reg =
"[a-zA-Z]+"
;
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(words);
HashMap<String, Integer> map =
new
HashMap<String, Integer>();
int
count =
0
;
while
(m.find()){
count++;
String w = m.group();
if
(
null
== map.get(w)){
map.put(w,
1
);
}
else
{
int
x = map.get(w);
map.put(w, x +
1
);
}
}
System.out.println(count);
System.out.println(map);
}
}
|
统计一段英文每个单词出现的次数
最新推荐文章于 2022-10-31 19:17:17 发布