此工具类需要依赖谷歌的java开发工具包guava,这个工具包里面具备了很多功能具体的可以参考并发编程网关于guava的系列教程[ 快捷入口 ]:
CaseFomat提供的枚举常量的说明
SN 枚举常量 说明
1 LOWER_HYPHEN 连字符的变量命名规范如lower-hyphen
2 LOWER_UNDERSCORE c++变量命名规范如lower_underscore
3 LOWER_CAMEL java变量命名规范如lowerCamel
4 UPPER_CAMEL java和c++类的命名规范如UpperCamel
5 UPPER_UNDERSCORE java和c++常量的命名规范如UPPER_UNDERSCORE
maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
例子
import org.junit.Test;
import com.google.common.base.CaseFormat;
public class GuavaTester {
@Test
public void test() {
System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data"));//testData
System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));//testData
System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));//TestData
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));//testdata
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));//test_data
System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));//test-data
}
}