思路:
/**
- 思路:1、先从字符串中的第一个字符开始遍历;
- 2、取出单个字符,使用正则表达式匹配数字;
- 如果匹配,则将这个数字字符转为整型,放入临时变量中;继续循环;
- 如果不匹配,则说明以这个字符开头的不是数字,就不用循环了,直接从下一个字符开始
*/
public void getMax() {
String str = "abc3d45678rd345";
int maxInt = 0;
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length() + 1; j++) {
String temp = str.substring(i, j);
if (temp.matches("[0-9]+")) {//正则匹配是否是数字字符串
int cur = Integer.parseInt(temp);
maxInt = cur > maxInt ? cur : maxInt;
} else
break;
}
}
System.out.println(maxInt);
}