美文网首页
判断一段String是否为数字

判断一段String是否为数字

作者: XM_Dong | 来源:发表于2018-04-26 16:03 被阅读4次

1、速度最快的方式-->正则

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

2、ASCII

    //判断一个字符串是否为数字
    public static boolean isNumeric3(String str){
        for(int i=str.length(); --i>=0;){
            char c=str.charAt(i);
            if (c < 48 || c > 57)
                return false;
        }
        return true;
    }

相关文章

网友评论

      本文标题:判断一段String是否为数字

      本文链接:https://www.haomeiwen.com/subject/ouzbxftx.html