java 怎么得到string字符串里面字母的个数
我试过这个方法 挺不错。
public static void test(String str){
Map map = new HashMap();
for(int i = 0; i < str.length(); i++){
String ch = str.charAt(i) + "";
Object och = map.get(ch);
if(och == null){
map.put(ch, new Integer(1));
}else{
map.put(ch, new Integer(((Integer)och).intValue()+ 1));
}
}
Iterator it = map.keySet().iterator();
System.out.println("字符串的总数是:" + str.length());
System.out.println("/的数量是 " + map.get("/"));
while(it.hasNext()){
// String key = (String)it.next();
System.out.println("字母 " + key + " 的数量是 " + map.get("/"));
}
}