实际业务中经常遇到需要将下划线转为驼峰命名的续期。下面的代码可以参考。
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(); sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : 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(); }