压缩输入字符串数据,比如输入3abb4d,则输出aaabbdddd
public String process(String str) {
String resStr = "";
int i = 0;//字符串索引
char c;//单个字符
while (i < str.length()) {
c = str.charAt(i);
if (c < 97) {
int num = str.charAt(i) - 48;
for (int j = 0; j < num - 1; j++) {//num-1去掉原字符串数字后的字符,3a
resStr += str.charAt(i + 1);
}
} else {
resStr += str.charAt(i);
}
i++;
}
return resStr;
}
public static void main(String[] args) {
System.out.println("请输入一组数据:");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
Test test = new Test();
//test.process(str);
System.out.println(test.process(str));
}
输出
请输入一组数据:
3a4d
aaadddd