我正在尝试在数字处拆分字符串。 例如,如果我有
String x = 2A2B;
我希望能够将字符串分成几部分,然后在屏幕上打印出“AABB”。谁能帮我解决这个问题吗?
最佳答案
下面的代码片段可能对您有帮助
String str = "3A2B";
// split the string on
String st[] = str.split("(?<=\\D)(?=\\d)");
System.out.println(Arrays.toString(st));
for (String s : st) {
String intValue = s.substring(0, s.length()-1);
int i = Integer.parseInt(intValue);
char c = s.charAt(s.length()-1);
while (i > 0) {
System.out.print(c);
i--;
}
System.out.println();
}
关于java - 按之间的数字分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27201378/