驼峰转下划线,下划线转驼峰和JSON字符串key键下划线转驼峰

下划线转驼峰

private  static Pattern linePattern = Pattern.compile("[_]\\w");
/**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(0).replace("_","").toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

驼峰转下划线

private  static Pattern humpPattern = Pattern.compile("[A-Z]");
/**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToLine(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();
    }

JSON字符串key键下划线转驼峰

private static Pattern jsonKeyPattern = Pattern.compile("_[\\w]+\":");
/**
     * JSON key 从下划线转为驼峰
     *
     * @param json
     * @return
     */
    public static String jsonKeyLineToHump(String json) {
        Matcher matcher = jsonKeyPattern.matcher(json);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            int index;
            StringBuilder builder = new StringBuilder(matcher.group(0));
            while ((index = builder.indexOf("_")) != -1) {
                String c = String.valueOf(builder.charAt(index+1));
                builder.replace(index, index + 2, c.toUpperCase());
            }
            matcher.appendReplacement(sb, builder.toString());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值