天天看点

查找字符串里每个字符出现的个数

查找字符串里每个字符出现的个数

public class HomeWork5 {
    public static void main(String[] args) {
        String msg = "snfdsnai2njdosa;jf;;;dpo;s;iaskjfpoakfpo34567567ksadpokdsDFYDFGHDFGHRTAYDKSOID";

        String newStr = "";//存储不重复的字母

        //找到不重复的单个字母
        for (int i = 0; i < msg.length(); i++) {
            char c = msg.charAt(i);
            if (newStr.indexOf(c) == -1) {
                newStr += c;
            }
        }
        System.out.println(newStr);

        //再去进行对比统计次数

        for(int i = 0;i<newStr.length();i++){

            //定义count进行数量统计
            int count = 0;

            for(int j = 0;j<msg.length();j++){
                if(newStr.charAt(i)==msg.charAt(j)){
                    count++;
                }
            }
            //输出结果
            System.out.println(newStr.charAt(i)+"出现了 "+ count);
        }

    }
}