1 Star 3 Fork 0

清风/CommonUtils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
BeanHump.java 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
清风 提交于 2019-06-03 16:00 . transform between camel and under line
import com.google.common.base.CaseFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.DigestUtils;
public class BeanHump {
public static final char UNDERLINE = '_';
public BeanHump() {
}
public static String camelToUnderline(String param) {
if (param != null && !"".equals(param.trim())) {
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for(int i = 0; i < len; ++i) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
if (i > 0 && param.charAt(i - 1) != '_') {
sb.append('_');
}
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
} else {
return "";
}
}
public static String underlineToCamel(String param) {
if (param != null && !"".equals(param.trim())) {
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for(int i = 0; i < len; ++i) {
char c = param.charAt(i);
if (c == '_') {
++i;
if (i < len) {
sb.append(Character.toUpperCase(param.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
} else {
return "";
}
}
public static String underlineToCamel2(String param) {
if (param != null && !"".equals(param.trim())) {
StringBuilder sb = new StringBuilder(param);
Matcher mc = Pattern.compile("_").matcher(param);
int var3 = 0;
while(mc.find()) {
int position = mc.end() - var3++;
String.valueOf(Character.toUpperCase(sb.charAt(position)));
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
}
return sb.toString();
} else {
return "";
}
}
public static void main(String[] args) {
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "aa_ABI2"));
System.out.println(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "aa_ABI2"));
System.out.println(camelToUnderline("currQuotaAppNum"));
System.out.println(DigestUtils.md5Hex("731555"));
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "first_Trans__TimeAgo"));
System.out.println(camelToUnderline("firstTransTimeAgo"));
System.out.println(underlineToCamel2("aa_bab_cac_a"));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/agp5050/CommonUtils.git
git@gitee.com:agp5050/CommonUtils.git
agp5050
CommonUtils
CommonUtils
master

搜索帮助