怎么把0-9的数字前面加上"_" 例如 "1"变为"_1"
(1)怎么把0-9的数字前面加上"_" 例如 "1"变为"_1" JAVA实现(2)怎么把字符串"_"变为"\UL"
(3)怎么把连续重复的字母 例如:eeeeee变为e5 --------------------编程问答-------------------- 1. System.out.println("ajfdslk4832904ksdfal43289".replaceAll("(?=\\d)", "_"));
2. System.out.println("fjaslkf_fdaskl_".replaceAll("_", "\\\\UL"));
--------------------编程问答-------------------- 学习一下正则 不难,或者简单一点自己写逻辑判断字符串 --------------------编程问答-------------------- 12题可以用正则,也可以用我的方法,这种问题都比较简单
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);
补充:Java , Java相关