使用StringUtils.isNumeric(String str) 判断string是否为数字

< P>检查字符串是否只包含Unicode数字。

*小数点不是Unicode数字,返回false。</P>

 <p> <代码> null <代码>将返回 false 。

*空字符串(“”)将返回 true </p>

方法实现: StringUtils.class

   * <pre>
     * StringUtils.isNumeric(null)   = false
     * StringUtils.isNumeric("")     = true
     * StringUtils.isNumeric("  ")   = false
     * StringUtils.isNumeric("123")  = true
     * StringUtils.isNumeric("12 3") = false
     * StringUtils.isNumeric("ab2c") = false
     * StringUtils.isNumeric("12-3") = false
     * StringUtils.isNumeric("12.3") = false
     * </pre>

 *@ PARAM str是要检查的字符串,可能为空

*@ 如果只包含数字,并且为非空 , 返回<code>true</code>

需要注意的是如果 str = ""  (入参为空字符串的情况下) 返回的是true .(根据需求检查入参str = "" 的情况)

public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/LMAKE_nbsp/article/details/83108112
今日推荐