public static void main(String[] args) throws MyException {
String str = "a_bef_cer_dsss_";
String str2 = lineToHump(str);
String str3 = humpToLine(str2);
System.out.println(str2);
System.out.println(str3);
}
/*下划线转驼峰*/
public static String lineToHump(String str){
String lowCase = str.toLowerCase();
Pattern pattern = Pattern.compile("_(\\w){1}");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
//matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
String upperCase = matcher.group(1).toUpperCase();
int start = matcher.start();
int end = matcher.end();
str = str.replace(matcher.group(), upperCase);
}
//matcher.appendTail(sb);
return str;
}
//*驼峰转下划线
public static String humpToLine(String str){
/**
* $0 = matcher.group(0)
*/
return str.replaceAll("[A-Z]", "_$0").toLowerCase();
}
public static String humpToLine2(String str){
Pattern pattern = Pattern.compile("[A-Z]");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(sb,"_"+matcher.group().toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}