1 public class HumpUtils { 2 private static final Pattern linePattern = Pattern.compile("_(\w)"); 3 private static final Pattern humpPattern = Pattern.compile("[A-Z]"); 4 5 /**下划线转驼峰*/ 6 public static String lineHump(String str){ 7 str = str.toLowerCase(); 8 Matcher matcher = linePattern.matcher(str); 9 StringBuffer sb = new StringBuffer(); 10 while(matcher.find()){ 11 matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); 12 } 13 matcher.appendTail(sb); 14 return sb.toString(); 15 } 16 /**驼峰转下划线*/ 17 public static String hump2Line(String str){ 18 Matcher matcher = humpPattern.matcher(str); 19 StringBuffer sb = new StringBuffer(); 20 while(matcher.find()){ 21 matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase()); 22 } 23 matcher.appendTail(sb); 24 return sb.toString(); 25 } 26 public static void main(String[] args) { 27 String lineToHump = lineHump("45hhf_parent_no_leader"); 28 System.out.println(lineToHump); 29 System.out.println(hump2Line(lineToHump)); 30 } 31 }