统计字符串里某个字符的个数:
import java.util.HashMap;
import java.util.StringTokenizer;
public class wordCount {
public static HashMap wordCount(String string) {
//用HashMap存放<单词:词频>这样一个映射关系
HashMap hashMap=new HashMap();
//用正则表达式来过滤字符串中的所有标点符号
String regex = "[【】、.。,"!--;:?'\]";
string = string.replaceAll(regex, " ");
//使用StringTokenizer来分词(StringTokenizer详见JDK文档)
StringTokenizer tokenizer = new StringTokenizer(string);
while(tokenizer.hasMoreTokens()){
String word=tokenizer.nextToken();
if(!hashMap.containsKey(word)){
hashMap.put(word, new Integer(1));
}else{
int k=hashMap.get(word).intValue()+1;
hashMap.put(word, new Integer(k));
}
}
//遍历HashMap,输出结果
return hashMap;
}
public static void main(String[]args) {
//转化成小写字符串
String string = "要统计的文本内容".toLowerCase();
wordCount test = new wordCount();
HashMap hashMap = test.wordCount(string);
//将hashmap的内容按字典序列输出
Collection keyset= hashMap.keySet();
List list = new ArrayList(keyset);
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println( list.get(i) + ":" + hashMap.get(list.get(i)) );
}
}
}



