private static final String NUMERIC_EXP = "([-,+]+[0-9]*(\\.?)[0-9]+)|([-,+]+[0-9]+)";
/**
* 驗證字符串是否爲整數、浮點數.
* @param str
* @return
*/
public static boolean isNumeric(String str) {
if(isBlank(str)){
return false;
}
if (str.matches(NUMERIC_EXP)) {
return true;
} else {
return false;
}
}
注:不推薦使用try{}catch(NumberFormatException e){}進行異常捕獲 .