您的位置:首页 > 其它

如何判断一个字符串是不是纯数字

2013-11-20 15:41 309 查看
代码:
public class NumberDemo {
public static void main(String[] args) {
String str1="1122.2.2";
String str2="111";
String str3="111.2";
String str4="111s";
String str5="111.s";
String str6="1s11";
System.out.println(str1+":"+isNum(str1));
System.out.println(str2+":"+isNum(str2));
System.out.println(str3+":"+isNum(str3));
System.out.println(str4+":"+isNum(str4));
System.out.println(str5+":"+isNum(str5));
System.out.println(str6+":"+isNum(str6));
}
public static boolean isNum(String str){
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");

}
}
结果:
1122.2.2:false
111:true
111.2:true
111s:false
111.s:false
1s11:false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: