下划线驼峰互转高效率方法

下划线驼峰互转高效率方法

  1. 第一种是网上比较常见的正则转换法
    效率较低
  2. 第二种是自写
    下划线转驼峰其效率为上面的2-4倍之间
    驼峰转下划线效率为正则的4倍以上

废话不多说,上代码

	public class HLUtil {
    private static final Pattern linePattern = Pattern.compile("_(\\w)");
    private static final Pattern humpPattern = Pattern.compile("[A-Z]");


    public static void main(String[] args) {
        int size = 100 * 10000;
        List<String> stringList1 = new ArrayList<>();
        List<String> stringList2 = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String s = "aa_sda_as_das_dsfs_xcvz_sef_wer_sdf_sdf_ds_sdf_sdf_rwer_ew_rew_sds_";
            stringList1.add(s);
            stringList2.add(fastLine2Hump(s));
        }

        for (int i = 0; i < 10; i++) {
            long start1 = System.currentTimeMillis();
            for (String str : stringList1) {
                fastLine2Hump(str);
            }
            long time1 = System.currentTimeMillis() - start1;

            long start2 = System.currentTimeMillis();
            for (String str : stringList2) {
                fastHump2Line(str);
            }
            long time2 = System.currentTimeMillis() - start2;

            long start3 = System.currentTimeMillis();
            for (String str : stringList1) {
                line2Hump(str);
            }
            long time3 = System.currentTimeMillis() - start3;

            long start4 = System.currentTimeMillis();
            for (String str : stringList2) {
                hump2Line(str);
            }
            long time4 = System.currentTimeMillis() - start4;

            System.out.println(time1 + " ** " + time2 + " ** " + time3 + " ** " + time4);
        }

    }

    /**
     * 下划线转驼峰
     */
    public static String line2Hump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * 驼峰转下划线
     */
    public static String hump2Line(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 高效率下划线转驼峰
     */
    public static String fastLine2Hump(String name) {
        StringBuilder sb = new StringBuilder(name);
        int length = sb.length();
        for (int i = 0; i < length - 1; i++) {
            if (sb.charAt(i) == 95) {
                char c = sb.charAt(i + 1);
                if (c > 96 && c < 123) {
                    sb.replace(i, i + 2, new String(new char[]{(char) (c - 32)}));
                } else {
                    sb.deleteCharAt(i);
                }
                length--;
            }
        }
        if (sb.charAt(length - 1) == 95) {
            sb.deleteCharAt(length - 1);
        }
        return sb.toString();
    }

    /**
     * 高效率驼峰转下划线
     */
    public static String fastHump2Line(String name) {
        StringBuilder sb = new StringBuilder(name);
        int length = sb.length();
        for (int i = 0; i < length; i++) {
            char c = sb.charAt(i);
            if (c > 64 && c < 91) {
                sb.replace(i, i + 1, new String(new char[]{95, (char) (c + 32)}));
                length++;
                i++;
            }
        }
        return sb.toString();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值