Java字符串转为驼峰格式构建工具类

2年前 (2022) 程序员胖胖胖虎阿
170 0 0

字符串转为驼峰格式 构建工具类

package com.yt.common.util;
import com.yt.common.dto.NameCode;
import com.yt.exam.enums.ZyEnum;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串转为驼峰格式 构建工具类
 * @author LYY
 * @date 2022/07/06
 */

public class RegexUtils {

    private static Pattern UNDERLINE_PATTERN = Pattern.compile("_([a-z])");


    public static void main(String[] args) {
        List<NameCode> nameCode = ZyEnum.getfield();
        for (int i = 0; i <nameCode.size() ; i++) {
            System.out.println(underlineToHump(nameCode.get(i).getField()));
        }
    }

    public static String underlineToHump (String str){
        //正则匹配下划线及后一个字符,删除下划线并将匹配的字符转成大写
        Matcher matcher = UNDERLINE_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if (matcher.find()) {
            sb = new StringBuffer();
            //将当前匹配的子串替换成指定字符串,并且将替换后的子串及之前到上次匹配的子串之后的字符串添加到StringBuffer对象中
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
            //把之后的字符串也添加到StringBuffer对象中
            matcher.appendTail(sb);
        } else {
            //去除除字母之外的前面带的下划线
            return sb.toString().replaceAll("_", "");
        }
        return underlineToHump(sb.toString());
    }


}

版权声明:程序员胖胖胖虎阿 发表于 2022年10月21日 上午6:16。
转载请注明:Java字符串转为驼峰格式构建工具类 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...