问题
	@Testpublic void isNumeric1() {System.out.println("h185  ===>" + isNumeric1("h185"));System.out.println("185h  ===>" + isNumeric1("185h"));System.out.println("-1    ===>" + isNumeric1("-1"));System.out.println("-1.85 ===>" + isNumeric1("-1.85"));System.out.println("1.85  ===>" + isNumeric1("1.85"));System.out.println("185   ===>" + isNumeric1("185"));}/*** 字符是否是数值* 问题:采用正则表达式的方式来判断一个字符串是否为数字,这种方式判断面比较全面,可以判断正负、整数小数 (推荐)* //?:0或1个, *:0或多个, +:1或多个** @param str* @return*/public boolean isNumeric1(String str) {if (StringUtils.isEmpty(str)) {return false;}Boolean strResult = str.matches("-?[0-9]+.?[0-9]*");if (!strResult) {return false;}return true;}
结果
h185  ===>false
185h  ===>true
-1    ===>true
-1.85 ===>true
1.85  ===>true
185   ===>true
处理
	@Testpublic void isNumeric() {System.out.println("h185   ===>" + isNumeric("h185"));System.out.println("185h   ===>" + isNumeric("185h"));System.out.println("-1     ===>" + isNumeric("-1"));System.out.println("-1.85  ===>" + isNumeric("-1.85"));System.out.println("1.85   ===>" + isNumeric("1.85"));System.out.println("185    ===>" + isNumeric("185"));}/*** 字符是否是数值** @param str* @return*/public boolean isNumeric(String str) {if (StringUtils.isEmpty(str)) {return false;}//import org.apache.commons.lang3.math.NumberUtils;Boolean strResult = NumberUtils.isParsable(str);if (!strResult) {return false;}return true;}
结果
h185   ===>false
185h   ===>false
-1     ===>true
-1.85  ===>true
1.85   ===>true
185    ===>true
查阅
java判断一个字符串是否为数字型