形如 productTypeId(驼峰风格),若要转换为 product_type_id(下划线风格),则可使用如下方法:
// 将驼峰风格替换为下划线风格public static String CamelhumpToUnderline(String str) { Matcher matcher = Pattern.compile("[A-Z]").matcher(str); StringBuilder builder = new StringBuilder(str); for (int i = 0; matcher.find(); i++) { builder.replace(matcher.start() + i, matcher.end() + i, "_" + matcher.group().toLowerCase()); } if (builder.charAt(0) == '_') { builder.deleteCharAt(0); } return builder.toString();}若要反过来,将下划线风格转换为驼峰风格,则可使用如下方法:
// 将下划线风格替换为驼峰风格public static String underlineToCamelhump(String str) { Matcher matcher = Pattern.compile("_[a-z]").matcher(str); StringBuilder builder = new StringBuilder(str); for (int i = 0; matcher.find(); i++) { builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase()); } if (Character.isUpperCase(builder.charAt(0))) { builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0)))); } return builder.toString();}