转自http://www.blogjava.net/hardson/archive/2006/10/11/58476.html
整理得很好,总结.
整理得很好,总结.
//用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
//用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
//用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;
}
本文介绍了三种使用Java进行字符串是否为数字的有效性验证的方法:利用Java自带的函数、使用正则表达式以及通过ASCII码判断。这些方法对于进行数据有效性检查非常实用。

358

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



