- 使用正则表达式
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
- 使用
// 注意空字符串的返回值,不同版本下有差异
boolean result = StringUtils.isNumeric("a123");
- 捕获NumberFormatException异常
public static boolean isNumeric(String str)
{
try{
(str);
return true;
}catch(NumberFormatException e){
return false;
}
}
- 使用()
public static boolean isNumeric(String str) {
for (int i = 0; i < (); i++) {
if (!((i))) {
return false;
}
}
return true;
}
原文链接:/guop/p/