java转换下划线为驼峰的2中方式
public static void main(String[] args) {
System.out.println(patternHump("sys_user_id"));
System.out.println(hump("sys_user_id"));
}
public static String hump(String str) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '_') {
if (i == chars.length - 1) {
break;
}
if (chars[i + 1] >= 97 && chars[i+1] <= 122) {
int upper = chars[i + 1] - (2 << 4);
chars[i + 1] = (char) upper;
}
}
}
return new String(chars).replace("_","");
}
static Pattern pattern = Pattern.compile("_(.)");
public static String patternHump(String str) {
StringBuffer sb = new StringBuffer();
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String group = matcher.group(1);
matcher.appendReplacement(sb, group.toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}