2008-03-19
java中判断字符串是否为数字的三种方法
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;
}
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;
}
发表评论
- 浏览: 22915 次
- 性别:

- 来自: 青岛

- 详细资料
搜索本博客
我的相册
111 046
共 4 张
共 4 张
最新评论
-
JAVA数据类型转换
谢谢楼主,说的很详细,好好学习下!
-- by sky21 -
JAVA数据类型转换
虽然是基础的东西, 但能总结出来还不错, 虽然有某些欠缺
-- by zhanjia -
15位和18位身份证的正则表 ...
不错,我收藏了
-- by jasongreen -
15位和18位身份证的正则表 ...
不错,我收藏了
-- by jasongreen -
DOS中 Orcle9i服务的启动 ...
呵呵
-- by jiake0504






评论排行榜