0%

guava库处理驼峰

Guava库处理驼峰

Guava提供了一个类用于字符串规范的转换

CaseFomat:

枚举常量 说明
LOWER_HYPHEN 连字符的变量命名规范如lower-hyphen
LOWER_UNDERSCORE c++变量命名规范如lower_underscore
LOWER_CAMEL java变量命名规范如lowerCamel
UPPER_CAMEL java和c++类的命名规范如UpperCamel
UPPER_UNDERSCORE java和c++常量的命名规范如UPPER_UNDERSCORE

maven依赖:

1
2
3
4
5
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>

测试:

1
2
3
4
5
6
7
8
9
10
11
@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

}