Java 判断字符串是否为正整数或者浮点数

    /**
     * 判断字符串是否为正整数或者浮点数
     * 1.11 -> true
     * 1 -> true
     * -1 -> false
     * 1a -> false
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        // 判断正整数正则
        Pattern patternInteger = Pattern.compile("^\\d+(\\.\\d+)?$");
        // 判断浮点数正则
        Pattern patternFloat = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
        return patternFloat.matcher(str).matches() || patternInteger.matcher(str).matches();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章