String:输入字符串“aaabbcc”变成“a3b2c2”

public static String test4(String str) {
        String newString = new String();
        char last = str.charAt(0);
        int repeatCount = 1;
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == last) {
                repeatCount++;
            } else {
                newString += last + "" + repeatCount;
                last = str.charAt(i);
                repeatCount = 1;

            }

        }

        return newString + last + repeatCount;
    }

你可能感兴趣的:(String:输入字符串“aaabbcc”变成“a3b2c2”)