使用正则表达式
public boolean isNumer(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
判断ASCII码值
public static boolean isNumer(String str)
{
for(int i=str.length();--i>=0;)
{
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
public static boolean isNumer(String str)
{
try{
Integer.parseInt(str);
return true;
}catch(NumberFormatException e)
{
System.out.println("数字类型转换错误:"+str);
return false;
}
}
public static boolean isNumeric00(String str)
{
try{
Integer.parseInt(str);
return true;
}catch(NumberFormatException e)
{
System.out.println("数字类型转换错误:"+str);
return false;
}
}
import cn.hutool.core.util.NumberUtil;
public static boolean isNumeric(String str)
{
return NumberUtil.isNumber(xx);
}
评论区