驼峰/下划线/中划线互转工具类

/**
 * 驼峰转下划线工具类
 */
@Slf4j
public class CamelUnderlineUtil {

    public static void main(String[] args) {
        List<String> resultList= new ArrayList();
        List<String> list= new ArrayList();
        list.add("ertUpopk");
        log.info("param is {}============================",list.get(0));
        camel2UnderScoreOrHyphen(list, resultList);
        list.clear();
        list.add("ert_upopk");
        log.info("param is {}============================",list.get(0));
        underScore2CamelOrHyphen(list, resultList);
        list.clear();
        list.add("ert-upopk");
        log.info("param is {}============================",list.get(0));
        hyphen2CamelOrUnderScore(list, resultList);

    }


    /**
     *驼峰转下划线及中划线
     * @param list
     * @param resultList
     */
    private static void camel2UnderScoreOrHyphen(List<String> list, List<String> resultList) {
        for (String key : list) {
            //驼峰转小写下划线
            log.info("----------驼峰转{}{}------------start","下划线","首字母小写");
            String to = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key);
            log.info(to);
            to = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, key);
            log.info(to);
            log.info("----------驼峰转{}{}------------end","下划线","首字母小写");

            log.info("----------驼峰转{}{}------------start","大写下划线","");
            to = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, key);
            log.info(to);
            to = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, key);
            log.info(to);
            log.info("----------驼峰转大{}{}------------end","大写下划线","");

            log.info("----------驼峰转{}{}------------start","小写中划线","");
            to = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, key);
            log.info(to);
            to = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, key);
            log.info(to);
            log.info("----------驼峰转{}{}------------end","小写中划线","");

            resultList.add(to);
        }
    }

    /**
     *下划线转驼峰及中划线
     * @param list
     * @param resultList
     */
    private static void underScore2CamelOrHyphen(List<String> list, List<String> resultList) {
        for (String key : list) {

            log.info("----------下划线转{}{}------------start","驼峰","首字母小写");
            String to = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, key);
            log.info(to);
            to = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, key);
            log.info(to);
            log.info("----------下划线转{}{}------------end","驼峰","首字母小写");

            log.info("----------下划线转{}{}------------start","驼峰","首字母大写");
            to = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, key);
            log.info(to);
            to = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, key);
            log.info(to);
            log.info("----------下划线转{}{}------------end","驼峰","首字母大写");

            log.info("----------下划线转{}{}------------start","小写中划线","");
            to = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, key);
            log.info(to);
            log.info("----------下划线转{}{}------------end","小写中划线","");
            resultList.add(to);
        }
    }


    /**
     *中划线转驼峰及下划线
     * @param list
     * @param resultList
     */
    private static void hyphen2CamelOrUnderScore(List<String> list, List<String> resultList) {
        for (String key : list) {

            log.info("----------中划线转{}{}------------start","驼峰","首字母小写");
            String to = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
            log.info(to);
            log.info("----------中划线转{}{}------------end","驼峰","首字母小写");

            log.info("----------中划线转{}{}------------start","驼峰","首字母大写");
            to = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, key);
            log.info(to);
            log.info("----------中划线转{}{}------------end","驼峰","首字母大写");

            log.info("----------中划线转{}{}------------start","小写下划线","");
            to = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_UNDERSCORE, key);
            log.info(to);
            log.info("----------中划线转{}{}------------end","小写下划线","");

            log.info("----------中划线转{}{}------------start","大写下划线","");
            to = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, key);
            log.info(to);
            log.info("----------中划线转{}{}------------end","大写下划线","");
            resultList.add(to);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值