//1.
public static boolean isNumeric(String string){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(string).matches();
}
//2.
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
//3.
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;
}

本文介绍了三种不同的字符串数字验证方法:使用正则表达式、字符遍历及ASCII码比较。这三种方法各有优缺点,适用于不同场景下的数字字符串验证。

2090

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



