JAVA下划线、驼峰相互转换

2021-03-14 13:29

阅读:401

标签:match   tail   mpi   ringbuf   pil   lin   private   ret   code   

 

 

 /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(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();
    }

    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();
    }

 

JAVA下划线、驼峰相互转换

标签:match   tail   mpi   ringbuf   pil   lin   private   ret   code   

原文地址:https://www.cnblogs.com/pxblog/p/14025739.html


评论


亲,登录后才可以留言!