Java判断字符串是数字(转)

时间:2025-03-11 22:11:29
  1. 使用正则表达式
public boolean isNumeric(String str){
       Pattern pattern = Pattern.compile("[0-9]*");
       Matcher isNum = pattern.matcher(str);
       if( !isNum.matches() ){
           return false;
       }
       return true;
}
  1. 使用
// 注意空字符串的返回值,不同版本下有差异
boolean result = StringUtils.isNumeric("a123");
  1. 捕获NumberFormatException异常
public static boolean isNumeric(String str)
{
  try{
     (str);
     return true;
  }catch(NumberFormatException e){
    return false;
  }
}
  1. 使用()
public static boolean isNumeric(String str) {
    for (int i = 0; i < (); i++) {
        if (!((i))) {
            return false;
        }
    }
    return true;
}

原文链接:/guop/p/