1用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
3用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
开发者博客:www.developsearch.com
本文介绍了三种使用Java进行字符串是否为数字的有效性验证的方法:利用Java自带的函数Character.isDigit进行逐字符判断;采用正则表达式Pattern匹配数字;以及通过ASCII码范围检查字符是否为数字。
8315

被折叠的 条评论
为什么被折叠?



