附测试样例。
上代码:
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;
public class StringTest {
/**
* 驼峰式命名法
*/
public static String toCamelStyle(String input) {
if (input == null) {
return null;
}
if ("".equalsIgnoreCase(input)) {
return input;
}
Pattern compile = Pattern.compile("^_*([^_].*[^_])_*$");
Matcher matcher = compile.matcher(input);
if (matcher.find()) {
input = matcher.group(1);
} else {
// 如果只有下划线则返回 空字符串
return "";
}
StringBuilder sb = new StringBuilder();
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
boolean isUnderline = '_' == chars[i];
if (isUnderline) {
// 下划线的后一个字符转大写,丢弃下划线
chars[i + 1] = Character.toUpperCase(chars[i + 1]);
} else {
if (i == 0) {
sb.append(Character.toLowerCase(chars[i]));
} else {
// (当前是大写 && ( (前一个是大写 && 后一个是大写) || (是最后一个字符 && 前一个是大写) ) )
if (Character.isUpperCase(chars[i]) &&
(
(i < chars.length - 1 && Character.isUpperCase(chars[i - 1]) && (Character.isUpperCase(chars[i + 1])))
||
(i == chars.length - 1 && Character.isUpperCase(chars[i - 1]))
)
) {
sb.append(Character.toLowerCase(chars[i]));
} else {
sb.append(chars[i]);
}
}
}
}
return sb.toString();
}
/**
* 下划线式命名法
*/
public static String toUnderlineStyle(String input) {
if (input == null) {
return null;
}
if ("".equalsIgnoreCase(input)) {
return input;
}
Pattern compile = Pattern.compile("^_*([^_].*[^_])_*$");
Matcher matcher = compile.matcher(input);
if (matcher.find()) {
input = matcher.group(1);
} else {
// 如果只有下划线则返回 空字符串
return "";
}
StringBuilder sb = new StringBuilder();
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
boolean isUpper = Character.isUpperCase(chars[i]);
// ( ( 非首个字符 && 当前是大写 && 前一个是小写 ) || ( 非首个字符 && 非最后一个字符 && 当前是大写 && 后一个是小写 ) )
if ((i > 0 && isUpper && Character.isLowerCase(chars[i - 1])) || (i > 0 && i < chars.length - 1 && isUpper && Character.isLowerCase(chars[i + 1]))) {
// 前一个字符不是下划线
if (chars[i - 1] != '_') {
sb.append('_');
}
}
sb.append(Character.toLowerCase(chars[i]));
}
return sb.toString();
}
public void test(String[] arr, Function<String, String> func) {
Arrays.stream(arr).forEach(s -> {
System.out.printf("%s --> %s%n", s, func.apply(s));
});
}
@Test
public void testCases() {
String str1 = "test_abc";
String Str2 = "test_Abc";
String Str3 = "Test_abc";
String Str4 = "testAbc";
String Str5 = "TestAbc";
String Str6 = "testabc";
String Str7 = "Testabc";
String Str8 = "_test_abc";
String Str9 = "test_abc_";
String Str10 = "_Testabc";
String Str11 = "TestABC";
String Str12 = "TestABC";
String Str13 = "TESTAbc";
String Str14 = "TestAbcW";
String Str31="_";
String Str32="__";
String[] testArr = Stream.of(str1, Str2, Str3, Str4, Str5, Str6, Str7, Str8, Str9, Str10,Str11,Str12,Str13,Str14,Str31,Str32).toArray(String[]::new);
test(testArr, StrUtils::toCamelStyle);
System.out.println("===================================");
test(testArr, StrUtils::toUnderlineStyle);
}
}
测试结果:
test_abc --> testAbc
test_Abc --> testAbc
Test_abc --> testAbc
testAbc --> testAbc
TestAbc --> testAbc
testabc --> testabc
Testabc --> testabc
_test_abc --> testAbc
test_abc_ --> testAbc
_Testabc --> testabc
TestABC --> testAbc
TestABC --> testAbc
TESTAbc --> testAbc
TestAbcW --> testAbcW
_ -->
__ -->
===================================
test_abc --> test_abc
test_Abc --> test_abc
Test_abc --> test_abc
testAbc --> test_abc
TestAbc --> test_abc
testabc --> testabc
Testabc --> testabc
_test_abc --> test_abc
test_abc_ --> test_abc
_Testabc --> testabc
TestABC --> test_abc
TestABC --> test_abc
TESTAbc --> test_abc
TestAbcW --> test_abc_w
_ -->
__ -->