代码改变世界

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

2022-07-27 00:45  hikoukay  阅读(558)  评论(0编辑  收藏  举报
/**
 * 驼峰转下划线工具类
 */
@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);
        }
    }
}