1、下划线转驼峰

1 /**
 2  * 下划线转驼峰
 3  * @param str
 4  * @return
 5  */
 6 public static String lineToHump(String str) {
 7     Pattern linePattern = Pattern.compile("_(\\w)");
 8     str = str.toLowerCase();
 9     Matcher matcher = linePattern.matcher(str);
10     StringBuffer sb = new StringBuffer();
11     while (matcher.find()) {
12         matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
13     }
14     matcher.appendTail(sb);
15     return sb.toString();
16 }

 

2、驼峰转下划线

1 /**
 2  * 驼峰转下划线
 3  * @param str
 4  * @return
 5  */
 6 public static String humpToLine(String str) {
 7     Pattern humpPattern = Pattern.compile("[A-Z]");
 8     Matcher matcher = humpPattern.matcher(str);
 9     StringBuffer sb = new StringBuffer();
10     while (matcher.find()) {
11         matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
12     }
13     matcher.appendTail(sb);
14     return sb.toString();
15 }