java判断字符串是否是数字

正则表达式

代码如下:

    public static boolean isNum(String num){
    return  num.matches("(\\s)*([+-])?(([0-9]*\\.)?([0-9]+)|([0-9]+)(\\.[0-9]*)?)([eE][\\+-]?[0-9]+)?(\\s)*");
    }

利用BigDecimal的异常

  public static boolean isNum(String str){
        try {
            BigDecimal num = new BigDecimal(str);
        }catch (Exception e){//抛异常必定不是数字
            return false;
        }return true;
    }

判断字符是否是数字

Character.isDigit(ch)

判断字符是否是字母

boolean isLetter(char ch)

猜你喜欢

转载自www.cnblogs.com/cstdio1/p/12128841.html