第一个比较傻的做法
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
// String string = sc.next();
String string = "abcd12345ed125ss123058789";
int start = 0;
int end = 0;
int length = 0;
int maxLegth = 0;
int maxStart = 0;
int maxEnd = 0;
boolean last = false;
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if ((c >= '0' && c <= '9')) {// 当前是数字
length++;
if (last) {// 如果上一个也是数字 长度+1
if (i == string.length() - 1) {
if (length >= maxLegth) {
maxStart = start;
maxEnd = string.length();
maxLegth = length;
}
}
} else {// 如果不是
last = true;
start = i;
}
} else {
if (last) {
if (length >= maxLegth) {
maxStart = start;
maxEnd = i;
maxLegth = length;
}
}
last = false;
start = 0;
end = 0;
length = 0;
}
}
if (maxLegth == 0) {
System.out.println(0);
} else {
System.out.println(string.substring(maxStart, maxEnd));
System.out.println(maxLegth);
}
}