检测字符串中各个字符出现的次数(java实现)

Posted by 竹林品雨 on November 10, 2013
//字符串 字符出现个数统计
private static String charAccounts(String orginStr){
    char[] unDuplicateCharArray;
    int[] charAccounts;
    unDuplicateCharArray = removeDuplicateChar(orginStr).toCharArray();
    charAccounts = new int[unDuplicateCharArray.length];
    for(int i = 0; i < unDuplicateCharArray.length; i++){
        for(char c : orginStr.toCharArray()){
            if(unDuplicateCharArray[i] == c){
                charAccounts[i] += 1;
            }
        }
    }

    //已字符串形式输出
    String reStr = "";
    for(int i = 0; i< unDuplicateCharArray.length; i++){
        String s = unDuplicateCharArray[i]+ ":" + charAccounts[i]+" ";
        reStr += s;
    }
    return reStr;
}

removeDuplicateChar 参考链接: 合并字符串中重复出现的字符(java实现)