Java 判断字符串是否是数字的方法

  • 使用 Java自带的函数
public static boolean isNumeric (String str) {
    for (int i = str.length(); --i >=0) {
          if (!Character.isDigit(str.charAt(i))) {
                return false;
          }
    }
    return true;
}
  • 使用正则表达式

方法一:

public static boolean isNumeric(String str) {
    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
    return pattern.matcher(str).matches();
}

方法二:

public static boolean isNumeric(String str) {
    if (str != null && !"".equals(str.trim())) {
          return s.matches("^[0-9]*$");
    }
    return false;
}

方法三:

 public static boolean isNumeric (String str) {
    Pattern pattern = Pattern.compile("[0-9]*");
    return pattern.matcher(str).matcher();
}
  • 使用 ASCII 码
public static boolean isNumeric (String str) {
    for (int i = str.length(); --i>=0;) {
        int chr = str.charAt(i);
        if (chr < 48 || chr > 57) {
            return false;
        }
   }
   return true;
}
  • 判断是不是浮点型数据
public static boolean isDouble(String str) {
    Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
    return pattern.matcher(str).matches();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容