前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >统计一个字符串中每一个字符出现的次数

统计一个字符串中每一个字符出现的次数

作者头像
WindCoder
发布2020-01-23 20:55:24
6070
发布2020-01-23 20:55:24
举报
文章被收录于专栏:WindCoder

本文提供三种方案,本质都是先将字符串转为数组:

  • String.charAt(index)
  • String.split("")
  • String.toCharArray()

具体如下:

代码语言:javascript
复制
    /**
     * 通过String.charAt(index)获取字符串中的字符
     * @param str
     */
    public static void count(String str){
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        int total = 0;
        Character tmp = null;
        for (int i=0;i<str.length(); i++) {
            tmp = str.charAt(i);
            total = 1;
            if (map.containsKey(tmp)) {
                total = map.get(tmp)+1;
            }
            map.put(tmp, total);
        }
        PrintUtill.printlnRule();
        PrintUtill.println(map);
    }

    /**
     * String.charAt(index)的变种,将最后存储的Character类型转为String类型
     * @param str
     */
    public static void count2(String str){
        Map<String, Integer> map = new HashMap<String, Integer>();
        int total = 0;
        String tmp = null;

        String maxChar = str.charAt(0) + "";
        int maxToal = 1;

        for (int i=0;i<str.length(); i++) {
            tmp = str.charAt(i) + "";
            total = 1;
            if (map.containsKey(tmp)) {
                total = map.get(tmp)+1;
            }
            map.put(tmp, total);
            if (maxToal<total) {
                maxToal = total;
                maxChar = tmp;
            }
        }
        PrintUtill.printlnRule();
        PrintUtill.println(map);
        PrintUtill.println( "出现次数最多的字母是:" + maxChar + ",出现次数是:" + maxToal);
    }

    /**
     * 通过String.split("")将字符串直接切割成字符串数组
     * @param str
     */
    public static void count3(String str){
        Map<String, Integer> map = new HashMap<String, Integer>();
        int total = 0;
        String[] myStrs = str.split("");
        String tmp = null;
        for (int i=0;i<myStrs.length; i++) {
            tmp = myStrs[i];
            total = 1;
            if (map.containsKey(tmp)) {
                total = map.get(tmp)+1;
            }
            map.put(tmp, total);
        }
        PrintUtill.printlnRule();
        PrintUtill.println(map);
    }

    /**
     * 通过String.toCharArray()将字符串转为char数组
     * @param str
     */
    public static void count4(String str){
        Map<String, Integer> map = new HashMap<String, Integer>();
        int total = 0;
        char[] myStrs = str.toCharArray();
        String tmp = null;
        for (int i=0;i<myStrs.length; i++) {
            tmp = myStrs[i] + "";
            total = 1;
            if (map.containsKey(tmp)) {
                total = map.get(tmp)+1;
            }
            map.put(tmp, total);
        }
        PrintUtill.printlnRule();
        PrintUtill.println(map);
    }

相关下载

点击下载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关下载
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档