记录一波,工作使用到直接copy。
/***
*
* 将驼峰转为下划线
*
* @param str
* @return java.lang.String
* @author comapss
* @date 2022/5/9 0:01
* @since 1.0.0
**/
public static String humpToUnderline(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();
}
/***
*
* 将下划线转为驼峰
*
* @param str
* @return java.lang.String
* @author comapss
* @date 2022/5/9 0:01
* @since 1.0.0
**/
public static String underlineToHump(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();
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)