mirror of https://gitee.com/makejava/EasyCode.git
!8 $tool工具中增加hump2Underline(String str) 用于将驼峰字符串转下划线字符串
Merge pull request !8 from tangcent/feature/tool
This commit is contained in:
commit
3e92cd6cd1
|
@ -38,7 +38,8 @@ public class NameUtils {
|
|||
/**
|
||||
* 转驼峰命名正则匹配规则
|
||||
*/
|
||||
private final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
|
||||
private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
|
||||
private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+");
|
||||
|
||||
/**
|
||||
* 首字母大写方法
|
||||
|
@ -60,6 +61,29 @@ public class NameUtils {
|
|||
return StringUtils.uncapitalize(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰转下划线,全小写
|
||||
*
|
||||
* @param str 驼峰字符串
|
||||
* @return 下划线字符串
|
||||
*/
|
||||
public String hump2Underline(String str) {
|
||||
if (StringUtils.isEmpty(str)) {
|
||||
return str;
|
||||
}
|
||||
Matcher matcher = TO_LINE_PATTERN.matcher(str);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
if (matcher.start() > 0) {
|
||||
matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
|
||||
} else {
|
||||
matcher.appendReplacement(buffer, matcher.group(0).toLowerCase());
|
||||
}
|
||||
}
|
||||
matcher.appendTail(buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过java全名获取类名
|
||||
*
|
||||
|
|
|
@ -45,6 +45,7 @@
|
|||
getClsNameByFullName(String fullName) 通过包全名获取类名
|
||||
getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名)
|
||||
getClassName(String name) 将下划线分割字符串转驼峰命名(类名)
|
||||
hump2Underline(String str) 将驼峰字符串转下划线字符串
|
||||
append(Object... objs) 多个数据进行拼接
|
||||
newHashSet(Object... objs) 创建一个HashSet对象
|
||||
newArrayList(Object... objs) 创建一个ArrayList对象
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.sjhy.plugin.tool;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* GlobalTool相关测试
|
||||
**/
|
||||
public class GlobalToolTest {
|
||||
|
||||
@Test
|
||||
public void hump2Underline() {
|
||||
GlobalTool tool = GlobalTool.getInstance();
|
||||
String out;
|
||||
//正常
|
||||
System.out.println(out = tool.hump2Underline("asianInfrastructureInvestmentBank"));
|
||||
Assert.assertEquals("asian_infrastructure_investment_bank", out);
|
||||
//首字母大写
|
||||
System.out.println(out = tool.hump2Underline("AsianInfrastructureInvestmentBank"));
|
||||
Assert.assertEquals("asian_infrastructure_investment_bank", out);
|
||||
//已经是下划线字符串
|
||||
System.out.println(out = tool.hump2Underline("asian_infrastructure_investment_bank"));
|
||||
Assert.assertEquals("asian_infrastructure_investment_bank", out);
|
||||
//包含重复的大写字符
|
||||
System.out.println(out = tool.hump2Underline("AAsianIInfrastructureInvestmentBank"));
|
||||
Assert.assertEquals("aasian_iinfrastructure_investment_bank", out);
|
||||
//空字符串
|
||||
System.out.println(out = tool.hump2Underline(""));
|
||||
Assert.assertEquals("", out);
|
||||
//null
|
||||
System.out.println(out = tool.hump2Underline(null));
|
||||
Assert.assertEquals(null, out);
|
||||
//全大写字符串
|
||||
System.out.println(out = tool.hump2Underline("AASIANINFRASTRUCTUREINVESTMENTBANK"));
|
||||
Assert.assertEquals("aasianinfrastructureinvestmentbank", out);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue