算法实现-求给定字符串中最大的数字字符

2020-09-20  本文已影响0人  永不熄灭的火焰_e306

思路:
/**

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);
}
上一篇 下一篇

猜你喜欢

热点阅读