2 回答

TA贡献1827条经验 获得超9个赞
您可以使用 :
String str = "Abcd123";
if (str.replaceAll("\\D", "").length() > 0) { // check if the string contain numbers
str = str.replaceFirst("\\d", " $0"); // if yes put a space before the first degit
}
输出
Abcd 123

TA贡献1797条经验 获得超4个赞
你可以使用这个简单的逻辑:
String str = "Abcd123";
String newStr = "";
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))){
newStr = newStr + " " + str.substring(i);
break;
} else {
newStr = newStr + str.charAt(i);
}
}
System.out.println(newStr);
在这里,我们遍历字符串中的每个字符,并在找到第一个数字后立即中断循环。
输出 :
第 123 章
添加回答
举报