驼峰转为下划线

 

 

public class StingUtil {
    private final static Pattern BIG_COMPILE = Pattern.compile("[A-Z]");
    private final static Pattern SMALL_COMPILE = Pattern.compile("_[a-z]");

    public static void main(String[] args) {
    }

    /**
     * 将驼峰转为下划线
     *
     * @param str
     * @return java.lang.String
     */
    public static String humpToUnderline(String str) {
        Matcher matcher = BIG_COMPILE.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 将下划线转为驼峰
     *
     * @param str
     * @return java.lang.String
     */
    public static String underlineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = SMALL_COMPILE.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}

 

posted @ 2022-07-01 19:24  进击的小蔡鸟  阅读(270)  评论(0编辑  收藏  举报