我想知道是否有一个正则表达式模式,我可以用它将下划线和小写字母转换为大写字母。我正在尝试从SQL语句为java bean生成字段名。目前,DB列是
load_id,policy_id,policy_number
但我希望java字段名称是
loadId,policyId,policyNumber
我尝试过这个regex fiddle
答案 0 :(得分:9)
您可以使用:
String s = "load_id,policy_id,policy_number";
Pattern p = Pattern.compile( "_([a-zA-Z])" );
Matcher m = p.matcher( s );
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
System.out.println(sb.toString()); // loadId,policyId,policyNumber
答案 1 :(得分:8)
也许你想使用Google Guava:
<强>代码强>:
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
public class Main {
public static void main(String[] args) {
String str = "load_id,policy_id,policy_number";
for(String columnName : str.split(",")) {
System.out.println(LOWER_UNDERSCORE.to(LOWER_CAMEL, columnName));
}
}
}
<强>输出强>:
loadId
policyId
policyNumber
答案 2 :(得分:0)
import com.google.common.base.CaseFormat;
protected static String replaceDashesWithCamelCasing(String input){
return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, input);
}
答案 3 :(得分:0)