添加maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
测试用例
public class AnswerApp {
public static void main(String[] args) {
// 驼峰转下划线, userName -> user_name
Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);
// 输出: user_name
System.out.println(converter.convert("userName"));
System.out.println();
// 驼峰转连接符, userName -> user-name
converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN);
// 输出: user-name
System.out.println(converter.convert("userName"));
System.out.println();
// 驼峰转首字符大写驼峰, userName -> UserName
converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
// 输出: UserName
System.out.println(converter.convert("userName"));
System.out.println();
// 驼峰转大写下划线, userName -> USER_NAME
converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE);
// 输出: USER_NAME
System.out.println(converter.convert("userName"));
System.out.println();
}
}