Browse Source

优化代码

xiaochan 3 years ago
parent
commit
8648c95e80
1 changed files with 11 additions and 12 deletions
  1. 11 12
      src/main/java/thyyxxk/webserver/utils/StringUtil.java

+ 11 - 12
src/main/java/thyyxxk/webserver/utils/StringUtil.java

@@ -16,6 +16,10 @@ import java.util.regex.Pattern;
 @Slf4j
 public class StringUtil {
 
+    private static final Pattern CHINESE_CHARACTERS = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
+
+    private static final Pattern CARRY_ENGLISH = Pattern.compile(".*\\d+.*");
+
     public static boolean isBlank(String str) {
         return null == str || "".equals(str.trim());
     }
@@ -43,10 +47,7 @@ public class StringUtil {
     }
 
     public static String[] triageDeptString2Array(String deptCodes) {
-        return null == deptCodes ? new String[]{}
-                : deptCodes.substring(1, deptCodes.length() - 1)
-                .replaceAll("\"", "")
-                .split(",");
+        return null == deptCodes ? new String[]{} : deptCodes.substring(1, deptCodes.length() - 1).replaceAll("\"", "").split(",");
     }
 
     /**
@@ -89,13 +90,12 @@ public class StringUtil {
         if (isBlank(str)) {
             return "%";
         }
-        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
-        Matcher m = p.matcher(str);
+        Matcher m = CHINESE_CHARACTERS.matcher(str);
         if (m.find()) {
             return "%" + str + "%";
         }
-        Pattern p1 = Pattern.compile(".*\\d+.*");
-        Matcher m1 = p1.matcher(str);
+
+        Matcher m1 = CARRY_ENGLISH.matcher(str);
         if (m1.matches()) {
             return "%" + str + "%";
         }
@@ -106,8 +106,7 @@ public class StringUtil {
         if (isBlank(str)) {
             return "";
         }
-        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
-        Matcher m = p.matcher(str);
+        Matcher m = CHINESE_CHARACTERS.matcher(str);
         if (m.find()) {
             return str;
         }
@@ -136,7 +135,7 @@ public class StringUtil {
 
     /**
      * 字符串相似度算法
-     * */
+     */
     public static double getSimilarDegree(String source, String target) {
         Map<Character, int[]> vector = new HashMap<>();
         int[] charCountArray;
@@ -151,7 +150,7 @@ public class StringUtil {
         }
         for (char targetChar : target.toCharArray()) {
             if (vector.containsKey(targetChar)) {
-                vector.get(targetChar)[1] ++;
+                vector.get(targetChar)[1]++;
             } else {
                 charCountArray = new int[2];
                 charCountArray[1] = 1;