请帮帮我,我想transfrom字符串AABSSSD是2AB3SD(有人叫它是加密).这是我解决它的方式:
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)
但输出是, …