Browse Source

!59 修复驼峰转下划线拼接时,简称缩写作为前缀时,转换后首字母后追加了下划线,同时做了代码优化,利于阅读
Merge pull request !59 from xjt283525476/master

xjt283525476 6 years ago
parent
commit
4474c533e6
1 changed files with 23 additions and 25 deletions
  1. 23 25
      ruoyi-common/src/main/java/com/ruoyi/common/utils/StringUtils.java

+ 23 - 25
ruoyi-common/src/main/java/com/ruoyi/common/utils/StringUtils.java

@@ -259,38 +259,36 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils
     /**
      * 下划线转驼峰命名
      */
-    public static String toUnderScoreCase(String s)
-    {
-        if (s == null)
-        {
+    public static String toUnderScoreCase(String str) {
+        if (str == null) {
             return null;
         }
         StringBuilder sb = new StringBuilder();
-        boolean upperCase = false;
-        for (int i = 0; i < s.length(); i++)
-        {
-            char c = s.charAt(i);
-
-            boolean nextUpperCase = true;
-
-            if (i < (s.length() - 1))
-            {
-                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
+        //前置字符是否大写
+        boolean preCharIsUpperCase = true;
+        //当前字符是否大写
+        boolean curreCharIsUpperCase = true;
+        //下一字符是否大写
+        boolean nexteCharIsUpperCase = true;
+        for (int i = 0; i < str.length(); i++) {
+            char c = str.charAt(i);
+            if (i > 0) {
+                preCharIsUpperCase = Character.isUpperCase(str.charAt(i-1));;
+            } else {
+                preCharIsUpperCase = false;
             }
+            
+            curreCharIsUpperCase = Character.isUpperCase(c);
 
-            if ((i > 0) && Character.isUpperCase(c))
-            {
-                if (!upperCase || !nextUpperCase)
-                {
-                    sb.append(SEPARATOR);
-                }
-                upperCase = true;
+            if (i < (str.length() - 1)) {
+                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
             }
-            else
-            {
-                upperCase = false;
+            
+            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
+                sb.append(SEPARATOR);
+            }  else if ((i !=0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
+                sb.append(SEPARATOR);
             }
-
             sb.append(Character.toLowerCase(c));
         }