描述:通过Java判断一个字符串,是不是数字。这里包括正数、负数、浮点数、科学计数法
代码:
import java.util.regex.Pattern;public class Test {public static void main(String[] args) {System.out.println(isNumeric("12.23"));System.out.println(isNumeric("+10"));System.out.println(isNumeric("-0.12"));System.out.println(isNumeric("1.2E5"));System.out.println(isNumeric("1.2D5"));System.out.println(isNumeric("dasd"));}public static boolean isNumeric(String str) {if (null == str || "".equals(str)) {return false;}String regx = "[+-]*\\d+\\.?\\d*[Ee]*[+-]*\\d+";Pattern pattern = Pattern.compile(regx);boolean isNumber = pattern.matcher(str).matches();if (isNumber) {return isNumber;}regx = "^[-\\+]?[.\\d]*$";pattern = Pattern.compile(regx);return pattern.matcher(str).matches();}
}
执行结果: