以下是antolife给您的回答,不能保证100%正确,但可给您一个建议.谢谢!
// 正则方式
String content = "30年前";
Pattern pattern = Pattern.compile("^(\\d+)(.*)");
Matcher matcher = pattern.matcher(content);
if (matcher.matches()) {//数字开头
System.out.println(matcher.group(1));// =30
System.out.println(matcher.group(2));// =年前
}
// 简单模式
String source = "30年前";
int indexOfNum = 0;
for (; indexOfNum < source.length(); indexOfNum++) {
if (!Character.isDigit(source.charAt(indexOfNum))) {
break;
}
}
if (indexOfNum != 0) {// 数字开头
System.out.println(source.substring(0, indexOfNum));// =30
System.out.println(source.substring(indexOfNum));// =年前
}
参考资料:纯手工打造,请珍惜