驼峰转下划线 下划线转驼峰
作者:互联网
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 驼峰转换工具 */ public class HumpUtil { private static Pattern linePattern = Pattern.compile("_(\\w)"); /** * 驼峰转下划线 * * @param str * @return */ public static String humpToLine(String str) { return str.replaceAll("[A-Z]", "_$0").toLowerCase(); } /** * 下划线转驼峰 * * @param str * @return */ public static String lineToHump(String str) { str = str.toLowerCase(); Matcher matcher = linePattern.matcher(str); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); } matcher.appendTail(sb); return sb.toString(); } }
标签:return,String,驼峰,matcher,str,下划线 来源: https://www.cnblogs.com/i-tao/p/15816818.html