日期:2014-05-20 浏览次数:20661 次
String str = "a1a2a3a4";
StringBuffer sbuf = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
sbuf.append("_");
}
sbuf.append(c);
}
System.out.println(sbuf);
String str = "aaaaaabbbbbbcddddddadfffffffffffffffffddddddddddddddd";
StringBuffer sbuf = new StringBuffer();
char prevChar = str.charAt(0);
int count = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == prevChar) {
count++;
}
if (c != prevChar
------解决方案--------------------
i == str.length() - 1) {
sbuf.append(prevChar).append(count <= 1 ? "" : count);
prevChar = c;
count = 1;
}
}
System.out.println(sbuf);