小编Le *_*uoc的帖子

将字符串"AABSSSD"转换为"2AB3SD"

请帮帮我,我想transfrom字符串AABSSSD2AB3SD(有人叫它是加密).这是我解决它的方式:

public class TransformString {

    public static void main(String[] args) {
        String str = "AABSSSD";
        StringBuilder newStr = new StringBuilder("");
        char temp = str.charAt(0);
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (temp == str.charAt(i)) {
                count++;
            } else {
                newStr.append(count);
                newStr.append(temp);
                count = 0;
            }
            temp = str.charAt(i);
            if(i == (str.length() - 1)){
                newStr.append(str.charAt(i));
            }
        }
        String x = String.valueOf(newStr);
        x = x.replace("0", "");
        System.out.print(x);
    }
}
Run Code Online (Sandbox Code Playgroud)

但输出是, …

java string

6
推荐指数
1
解决办法
90
查看次数

标签 统计

java ×1

string ×1