top.isopen.commons.springboot.util.NameUtil Maven / Gradle / Ivy
package top.isopen.commons.springboot.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 命名工具类
*
* @author TimeChaser
* @version 1.0
* @since 2023/7/7 17:48
*/
public class NameUtil {
/**
* 驼峰转下划线
*
* @param str 目标字符串
* @return java.lang.String
*/
public static String humpToUnderline(String str) {
String regex = "([A-Z])";
Matcher matcher = Pattern.compile(regex).matcher(str);
while (matcher.find()) {
String target = matcher.group();
str = str.replaceAll(target, "_" + target.toLowerCase());
}
return str;
}
/**
* 下划线转驼峰
*
* @param str 目标字符串
* @return java.lang.String
*/
public static String underlineToHump(String str) {
String regex = "_(.)";
Matcher matcher = Pattern.compile(regex).matcher(str);
while (matcher.find()) {
String target = matcher.group(1);
str = str.replaceAll("_" + target, target.toUpperCase());
}
return str;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy