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

一、用 java 自带的函数

    /**
     * 判断一个 string 类型的字符串是不是一个数字
     *
     * @param str string 类型的字符串
     * @return <code>true</code> 该 string 类型的字符串是十进制正整数. 其他的会返回<code>false</code>
     */
    private static boolean judgeByToCharArray(final String str) {
        // null or empty
        if (str == null || str.length() == 0) {
            return false;
        }
        return str.chars().allMatch(Character::isDigit);
    }

二、使用正则表达式

    /**
     * 使用正则表达式判断字符串是否是数字
     *
     * @param str string 类型的字符串
     * @return <code>true</code> 该 string 是整数. 其他的会返回<code>false</code>
     */
    public static boolean isNumericByRegEx(String str) {
        // ?:0或1个, *:0或多个, +:1或多个 
        // 匹配所有整数
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        // 匹配小数
        Pattern pattern2 = Pattern.compile("^[-\\+]?[\\d]+[.][\\d]+$");
        return pattern.matcher(str).matches() || pattern2.matcher(str).matches();
    }

三、使用 ascii 码

    /**
     * 使用 ASCII 码判断字符串是否是数字
     *
     * @param str string 类型的字符串
     * @return <code>true</code> 该 string 类型的字符串是十进制正整数. 其他的会返回<code>false</code>
     */
    public static boolean isNumericByAscii(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        return str.chars().allMatch(chr -> (chr >= 48 && chr <= 57));
    }

四、parse 方法

    /**
     * 判断一个 string 类型的字符串是不是一个数字
     *
     * @param str string 类型的字符串
     * @return <code>true</code> 该 string 类型的字符串是十进制正整数. 其他的会返回<code>false</code>
     */
    public static boolean isNumeric(final String str) {
        // null or empty
        if (str == null || str.length() == 0) {
            return false;
        }
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            try {
                Double.parseDouble(str);
                return true;
            } catch (NumberFormatException ex) {
                try {
                    Float.parseFloat(str);
                    return true;
                } catch (NumberFormatException exx) {
                    return false;
                }
            }
        }
    }

五、第三方类库

    /**
     * 判断一个 string 类型的字符串是不是一个数字
     *
     * <p>
     * commons-lang3 库 -- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
     * </p>
     *
     * @param str string 类型的字符串
     * @return <code>true</code> 该 string 类型的字符串是数字
     */
    private static boolean judgeByCommonsLong3(final String str) {
        // NumberUtils.isParsable(str)
        // NumberUtils.isDigits(str)
        // 这一个效果好一点
        return NumberUtils.isCreatable(str);
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值