判断字符串中数字,空格,字母以及其他字符数量


//    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    //这里可以使用Character的一些方法更方便的判断
    public static Map<String, Integer> method7(String str) {
        Map<String, Integer> result = new HashMap<>();
        char[] chars = str.toCharArray();
        int dig = 0;//数字计数
        int blank = 0;//空格
        int word = 0;//字母
        int other = 0;//其他字符
        for (char ch : chars) {
            if (Character.isLetter(ch)) {
                word++;
            } else if (Character.isDigit(ch)) {
                dig++;
            } else if (Character.isSpaceChar(ch)) {
                blank++;
            } else {
                other++;
            }
        }
        result.put("dig", dig);
        result.put("blank", blank);
        result.put("word", word);
        result.put("other", other);
        return result;

    }


原文链接:https://www.cnblogs.com/wyq1995/p/13498694.html