首页 > 其他 > 详细

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

时间:2020-08-14 02:02:26      阅读:67      评论:0      收藏:0      [点我收藏+]
//    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    //这里可以使用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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!