架构师之数字判断-----------------怎么判断一个字符串是个数字

1.前言.

如题.

2.代码.

方法1:
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]+))?)$");
	}
}

摘自:http://zhidao.baidu.com/question/398578188.html

方法2:

用char大小判断.

方法3:

用apache的NumberUtils类,不推荐,已经不推荐使用.

方法4:

直接分类判断parse

相关推荐