java判断字符串是否为数字或小数

public static boolean isNumeric(String str){
    Pattern pattern = Pattern.compile("[0-9]*");
    if(str.indexOf(".")>0){//判断是否有小数点
        if(str.indexOf(".")==str.lastIndexOf(".") && str.split("\\.").length==2){ //判断是否只有一个小数点
            return pattern.matcher(str.replace(".","")).matches();
        }else {
            return false;
        }
    }else {
        return pattern.matcher(str).matches();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39936341/article/details/83114476