diff --git a/diboot-core/src/main/java/com/diboot/core/service/impl/DictionaryServiceImpl.java b/diboot-core/src/main/java/com/diboot/core/service/impl/DictionaryServiceImpl.java index c48896b..00a829b 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/impl/DictionaryServiceImpl.java +++ b/diboot-core/src/main/java/com/diboot/core/service/impl/DictionaryServiceImpl.java @@ -62,7 +62,6 @@ public class DictionaryServiceImpl extends BaseServiceImpl 0){ sb.append(Cons.SEPARATOR_UNDERSCORE); } - sb.append(Character.toLowerCase(c)); - } - else{ - sb.append(c); } + sb.append(Character.toLowerCase(c)); } return sb.toString(); } @@ -144,27 +141,31 @@ public class S extends StringUtils{ if(V.isEmpty(snakeCaseStr)){ return null; } - // 不包含_,直接return + // 不包含_ if(!snakeCaseStr.contains(Cons.SEPARATOR_UNDERSCORE)){ - return snakeCaseStr; - } - char[] chars = snakeCaseStr.toCharArray(); - StringBuilder sb = new StringBuilder(); - boolean upperCase = false; - for (char c : chars){ - if(Cons.SEPARATOR_UNDERSCORE.equals(Character.toString(c))){ - upperCase = true; - continue; - } - if(upperCase){ - sb.append(Character.toUpperCase(c)); - upperCase = false; + // 全大写直接return小写 + if(snakeCaseStr.toUpperCase().equals(snakeCaseStr)){ + return snakeCaseStr.toLowerCase(); } + // 其他return 首字母小写 else{ - sb.append(c); + return uncapFirst(snakeCaseStr); } } - return sb.toString(); + // 包含_ + String result = null; + String[] words = snakeCaseStr.split(Cons.SEPARATOR_UNDERSCORE); + for(String word : words){ + if(V.notEmpty(word)){ + if(result == null){ + result = word.toLowerCase(); + } + else{ + result += word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); + } + } + } + return result; } /*** @@ -285,7 +286,7 @@ public class S extends StringUtils{ */ public static String uncapFirst(String input){ if(input != null){ - return String.valueOf(input.charAt(0)).toLowerCase() + input.substring(1); + return Character.toLowerCase(input.charAt(0)) + input.substring(1); } return null; } @@ -296,7 +297,7 @@ public class S extends StringUtils{ */ public static String capFirst(String input){ if(input != null){ - return String.valueOf(input.charAt(0)).toUpperCase() + input.substring(1); + return Character.toUpperCase(input.charAt(0)) + input.substring(1); } return null; } diff --git a/diboot-core/src/test/java/diboot/core/test/util/STest.java b/diboot-core/src/test/java/diboot/core/test/util/STest.java index 729da6b..3e89a14 100644 --- a/diboot-core/src/test/java/diboot/core/test/util/STest.java +++ b/diboot-core/src/test/java/diboot/core/test/util/STest.java @@ -14,9 +14,10 @@ public class STest { @Test public void testToSnakeCase(){ - String str = "myOrgName"; + String camelCaseStr = "myOrgName"; String snakeCaseStr = "my_org_name"; - Assert.assertEquals(S.toSnakeCase(str), snakeCaseStr); + Assert.assertEquals(S.toSnakeCase(camelCaseStr), snakeCaseStr); + Assert.assertEquals(S.toSnakeCase(S.capFirst(camelCaseStr)), snakeCaseStr); } @Test @@ -24,6 +25,7 @@ public class STest { String snakeCaseStr = "my_org_name"; String camelCaseStr = "myOrgName"; Assert.assertEquals(S.toLowerCaseCamel(snakeCaseStr), camelCaseStr); + Assert.assertEquals(S.toLowerCaseCamel(snakeCaseStr.toUpperCase()), camelCaseStr); } @Test @@ -33,4 +35,4 @@ public class STest { Assert.assertEquals(S.uncapFirst("HelloWorld"), text); } -} +} \ No newline at end of file