Skip to content

Commit b37d1d0

Browse files
committed
完善checkUtil工具类
1 parent 4e558f7 commit b37d1d0

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/main/java/com/chen/util/CheckUtil.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.chen.util;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
/**
47
* 检查工具类
58
* Created by chenwj3 on 2017/1/18.
@@ -21,6 +24,7 @@ public static Boolean isEmpty(String o) {
2124

2225
/**
2326
* 判断字符串是不是数字
27+
*
2428
* @param str
2529
* @return
2630
*/
@@ -29,4 +33,29 @@ public static Boolean checkNum(String str) {
2933
return !isEmpty(str) && str.matches("^\\d+$");
3034
}
3135

36+
/**
37+
* 判断字符串是否为数字,包括整数和小数.
38+
*
39+
* @param str
40+
* @return
41+
*/
42+
public static boolean isNumeric(String str) {
43+
Pattern pattern = Pattern.compile("^[-]?[0.0-9.0]+$");
44+
Matcher isNum = pattern.matcher(str);
45+
if (!isNum.matches()) {
46+
return false;
47+
}
48+
return true;
49+
}
50+
51+
public static void main(String[] args) {
52+
System.out.println(isNumeric("1"));
53+
System.out.println(isNumeric("-1"));
54+
System.out.println(isNumeric("0"));
55+
System.out.println(isNumeric("-3.2"));
56+
System.out.println(isNumeric("3.2"));
57+
System.out.println(isNumeric("1.3456"));
58+
System.out.println(isNumeric("wqw12"));
59+
}
60+
3261
}

0 commit comments

Comments
 (0)