字符串截取

统计一句话中各个字符的个数 现有字符串"good good study, day day up.", 统计其中各个字符出现的次数。例如:上述字符串中各个字符的出现的次数为: {g=2, u=2, d=5, t=1, s=1, p=1, a=2,o=4, y=3}。

public static void main(String[] args) {
        String str = "good good  study, day day up.";
        str = str.replaceAll("[^a-zA-Z]+", "");
        System.out.println("好好学习天天向上:"+str);
        
        Map map = new HashMap();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i); //截取一个字符并存到c
            if (map.containsKey(c)) {// 判断集合中是否有该字符
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        System.out.println(map);

    }

你可能感兴趣的:(字符串截取)