/**
* 将驼峰转为下划线
*/
public static String underline(String str) {
Pattern compile = Pattern.compile("[A-Z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 将下划线转为驼峰
*/
public static String hump(String str) {
str = str.toLowerCase();
Pattern compile = Pattern.compile("_[a-z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_",""));
}
matcher.appendTail(sb);
return sb.toString();
}
Java驼峰下划线互转
最新推荐文章于 2023-12-02 12:15:00 发布