JAVA判断字符串是否为数字或者是否以0开头
来源:互联网 发布:本坦库尔数据 编辑:程序博客网 时间:2024/05/29 16:39
一、判断是否是数字
下面有四种方法,估计好用的应该是第三种吧。
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();
}
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.还是正则表达式
public static boolean isNumeric(String str){
if(str.matches("\\d*"){
return true;
}else{
return false;
}
}
4.用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;
}
if(str.matches("\\d*"){
return true;
}else{
return false;
}
}
4.用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;
}
参考:http://www.cnblogs.com/ztf2008/archive/2009/02/19/1393744.html
二、数字是否以0开头
String regex="^0\\d*$";
三、数字不以0开头
String regex="^[1-9]\\d*$";
- JAVA判断字符串是否为数字或者是否以0开头
- Java判断字符串是否以数字开头
- Java 判断字符串是否以什么开头
- iOS 判断字符串是否以字母或者汉字开头
- C# 判断一个字符串是否数字开头
- java取出以数字开头或者以数字结尾的字符串,将字符串转换为大写
- Java中判断字符串是否为数字
- JAVA判断字符串是否为数字
- JAVA判断字符串是否为数字
- java判断字符串是否为数字
- JAVA判断字符串是否为数字
- JAVA判断字符串是否为数字
- java判断字符串是否为数字
- Java判断字符串是否为数字
- JAVA判断字符串是否为数字
- JAVA判断字符串是否为数字
- JAVA判断字符串是否为数字
- JAVA判断字符串是否为数字
- offsetLeft你理解了吗?
- ecshop模板二次开发-ecshopdev小鸟(http://www.ecshopdev.com)
- java内部类
- 项目后的启发
- LSP The The Liskov Substitution Principle(里氏代换原则)
- JAVA判断字符串是否为数字或者是否以0开头
- 执行shell 文件为mongodb建立索引
- 简要描述ASP.NET的页面生命周期
- 韩寒 生存
- 实时竞价(RTB)介绍
- ibatis中任意sql取值
- Struts2&Hibernate项目经验
- 关于ioremap 和 phys_to_virt
- ActiveXObject函数详解