首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

java 如何得到string字符串里面字母的个数

2012-09-05 
java 怎么得到string字符串里面字母的个数我试过这个方法 挺不错。 public static void test(String str){M

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("/"));
      }
    }  

热点排行