1. 程式人生 > >java中判斷一個字串是否為整型數字

java中判斷一個字串是否為整型數字

/**
     * 判斷字串是否為數字
     */
    public static boolean isNumeric(String str){ 
        Pattern pattern = Pattern.compile("[0-9]*"); 
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();  
     }

測試

public static void main(String[] args) {
		String str = "11";
		String str1 = "11sdf";
		System.out.println(isNumeric(str));
		System.out.println(isNumeric(str1));
	}
輸出:
true
false