java判断某个字符串是否是数字

(一)利用正则表达式判断某个字符串是否是数字

    public static boolean isNumeric(String s) {
        // 正则表达式
        return (s.matches("\\d*") && Pattern.compile("[0-9]*").matcher(s)
                .matches());
    }

 

(二)利用格式转换异常来确定

    // 判断字符串是否为数字
    public boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

 

posted @ 2013-06-04 11:14  杨桃  阅读(254)  评论(0编辑  收藏  举报