瀏覽代碼

优化字符串数字判断逻辑

woody 7 月之前
父節點
當前提交
5bc04db3c8
共有 1 個文件被更改,包括 12 次插入7 次删除
  1. 12 7
      framework-common/src/main/java/com/chelvc/framework/common/util/StringUtils.java

+ 12 - 7
framework-common/src/main/java/com/chelvc/framework/common/util/StringUtils.java

@@ -86,11 +86,6 @@ public final class StringUtils {
      */
     public static final String LETTER_REGEX = "[a-zA-Z]+";
 
-    /**
-     * 整数正则表达式
-     */
-    public static final String INTEGER_REGEX = "-?[0-9]+";
-
     /**
      * 逗号拆分分隔符
      */
@@ -124,7 +119,7 @@ public final class StringUtils {
     /**
      * 数字正则表达式
      */
-    public static final String NUMBER_REGEX = "-?(\\.?[0-9]+|[0-9]+\\.?[0-9]+|[0-9]+\\.?)";
+    public static final String NUMBER_REGEX = "[\\+-]?(\\.?[0-9]+|[0-9]+\\.?[0-9]+|[0-9]+\\.?)";
 
     /**
      * URL正则表达式
@@ -432,7 +427,17 @@ public final class StringUtils {
      * @return true/false
      */
     public static <T extends CharSequence> boolean isInteger(T source) {
-        return notEmpty(source) && getPattern(INTEGER_REGEX).matcher(source).matches();
+        if (isEmpty(source)) {
+            return false;
+        }
+        char first = source.charAt(0);
+        int i = first == '+' || first == '-' ? 1 : 0;
+        for (int length = source.length(); i < length; i++) {
+            if (!Character.isDigit(source.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
     }
 
     /**