Java之驼峰和下划线命名的相互转化-yellowcong
程序员文章站
2022-06-01 07:50:17
...
在数据库命名为下划线大写的方式,我们需要通过驼峰命名转化工具,将名称转化为驼峰,然后生成java的entity,在这个工具包中,通过正则表达式的方式,进行匹配,然后进行命名转化
工具包
package com.yellowcong.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtils {
/**
* 下划线转驼峰法(默认小驼峰)
*
* @param line
* 源字符串
* @param smallCamel
* 大小驼峰,是否为小驼峰(驼峰,第一个字符是大写还是小写)
* @return 转换后的字符串
*/
public static String underline2Camel(String line, boolean ... smallCamel) {
if (line == null || "".equals(line)) {
return "";
}
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
Matcher matcher = pattern.matcher(line);
//匹配正则表达式
while (matcher.find()) {
String word = matcher.group();
//当是true 或则是空的情况
if((smallCamel.length ==0 || smallCamel[0] ) && matcher.start()==0){
sb.append(Character.toLowerCase(word.charAt(0)));
}else{
sb.append(Character.toUpperCase(word.charAt(0)));
}
int index = word.lastIndexOf('_');
if (index > 0) {
sb.append(word.substring(1, index).toLowerCase());
} else {
sb.append(word.substring(1).toLowerCase());
}
}
return sb.toString();
}
/**
* 驼峰法转下划线
*
* @param line
* 源字符串
* @return 转换后的字符串
*/
public static String camel2Underline(String line) {
if (line == null || "".equals(line)) {
return "";
}
line = String.valueOf(line.charAt(0)).toUpperCase()
.concat(line.substring(1));
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String word = matcher.group();
sb.append(word.toUpperCase());
sb.append(matcher.end() == line.length() ? "" : "_");
}
return sb.toString();
}
public static void main(String[] args) {
String line = "are_you_dou_bi_yellowcong";
//下划线转驼峰(大驼峰)
//AreYouDouBiYellowcong
String camel = underline2Camel(line, false);
System.out.println(camel);
//下划线转驼峰(小驼峰)
//areYouDouBiYellowcong
camel = underline2Camel(line);
System.out.println(camel);
//驼峰转下划线
//ARE_YOU_DOU_BI_YELLOWCONG
System.out.println(camel2Underline(camel));
}
}
测试结果,默认的是小驼峰的方法