驼峰和下划线命名转换-java完美版

附测试样例。
上代码:

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
_ --> 
__ --> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值