mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!464 StrUtil.toCamelCase 方法重载
Merge pull request !464 from handy/hs-dev
This commit is contained in:
commit
2dc120da02
@ -4022,6 +4022,19 @@ public class CharSequenceUtil {
|
||||
return NamingCase.toCamelCase(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将连接符方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。<br>
|
||||
* 例如:hello_world=》helloWorld; hello-world=》helloWorld
|
||||
*
|
||||
* @param name 转换前的下划线大写方式命名的字符串
|
||||
* @param symbol 连接符
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
* @see NamingCase#toCamelCase(CharSequence, char)
|
||||
*/
|
||||
public static String toCamelCase(CharSequence name, char symbol) {
|
||||
return NamingCase.toCamelCase(name, symbol);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------ isSurround
|
||||
|
||||
/**
|
||||
|
@ -150,19 +150,30 @@ public class NamingCase {
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String toCamelCase(CharSequence name) {
|
||||
return toCamelCase(name, CharUtil.UNDERLINE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将连接符方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。<br>
|
||||
*
|
||||
* @param name 转换前的自定义方式命名的字符串
|
||||
* @param symbol 连接符
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String toCamelCase(CharSequence name, char symbol) {
|
||||
if (null == name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final String name2 = name.toString();
|
||||
if (StrUtil.contains(name2, CharUtil.UNDERLINE)) {
|
||||
if (StrUtil.contains(name2, symbol)) {
|
||||
final int length = name2.length();
|
||||
final StringBuilder sb = new StringBuilder(length);
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < length; i++) {
|
||||
char c = name2.charAt(i);
|
||||
|
||||
if (c == CharUtil.UNDERLINE) {
|
||||
if (c == symbol) {
|
||||
upperCase = true;
|
||||
} else if (upperCase) {
|
||||
sb.append(Character.toUpperCase(c));
|
||||
@ -176,4 +187,5 @@ public class NamingCase {
|
||||
return name2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -384,6 +384,12 @@ public class StrUtilTest {
|
||||
|
||||
String abc1d = StrUtil.toCamelCase("abc_1d");
|
||||
Assert.assertEquals("abc1d", abc1d);
|
||||
|
||||
|
||||
String str2 = "Table-Test-Of-day";
|
||||
String result2 = StrUtil.toCamelCase(str2, CharUtil.DASHED);
|
||||
System.out.println(result2);
|
||||
Assert.assertEquals("tableTestOfDay", result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user