com.sinszm.common.util.CamelAndUnderLineConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of szm-boot-common Show documentation
Show all versions of szm-boot-common Show documentation
基于SpringBoot扩展生态应用公共组件
Copyright © 2020 智慧程序猿 All rights reserved.
https://www.sinsz.com
package com.sinszm.common.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串驼峰命名相互转换
*
* @author chenjianbo
*/
public final class CamelAndUnderLineConverter {
private static Pattern linePattern = Pattern.compile("_(\\w)");
private static Pattern humpPattern = Pattern.compile("[A-Z]");
/**
* 下划线转驼峰
* @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();
}
/**
* 驼峰转下划线,效率较高
* @param str 参数
* @return 下线字符串
*/
public static String humpToLine(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 驼峰转下划线(简单写法,效率低于{@link #humpToLine(String)})
* @param str 参数
* @return 下线字符串
*/
public static String humpToLine2(String str) {
return str.replaceAll("[A-Z]", "_$0").toLowerCase();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy