正则表达式
推荐网站
学习教程 http://www.zjmainstay.cn/
零宽断言
https://cisy.me/zero-width-assertions/
1 private static Pattern linePattern = Pattern.compile("_(\\w)"); 2 private static Pattern humpPattern = Pattern.compile("[A-Z]"); 3 4 /** 5 * 驼峰转下划线,最后转为大写 6 * @param str 7 * @return 8 */ 9 public static String humpToLine(String str) { 10 Matcher matcher = humpPattern.matcher(str); 11 StringBuffer sb = new StringBuffer(); 12 while (matcher.find()) { 13 matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase()); 14 } 15 matcher.appendTail(sb); 16 return sb.toString().toUpperCase(); 17 } 18 19 /** 20 * 下划线转驼峰,正常输出 21 * @param str 22 * @return 23 */ 24 public static String lineToHump(String str) { 25 Matcher matcher = linePattern.matcher(str); 26 StringBuffer sb = new StringBuffer(); 27 while (matcher.find()) { 28 matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); 29 } 30 matcher.appendTail(sb); 31 return sb.toString(); 32 } 33 /** 34 *———————————————— 35 *版权声明:本文为CSDN博主「dr.liuzg」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 36 *原文链接:https://blog.csdn.net/weixin_42958809/java/article/details/89135467 37 */
核心代码为 matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
--------蓝天上的云_转载请注明出处.