java中驼峰命名和下划线命名互转方法(代码实现)
1 /** 2 * 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。 3 * 例如:HelloWorld->HELLO_WORLD 4 * @param name 转换前的驼峰式命名的字符串 5 * @return 转换后下划线大写方式命名的字符串 6 */ 7 public static String underscoreName(String name) { 8 StringBuilder result = new StringBuilder(); 9 if (name != null && name.length() > 0) { 10 // 将第一个字符处理成大写 11 result.append(name.substring(0, 1).toUpperCase()); 12 // 循环处理其余字符 13 for (int i = 1; i < name.length(); i++) { 14 String s = name.substring(i, i + 1); 15 // 在大写字母前添加下划...