驼峰字符串和下划线字符串格式如何相互转换?

158 阅读1分钟

引入依赖

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

代码结构

image.png

常用方法

CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_UNDERSCORE,"family_name_and_last_name");
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE,"family_name_and_last_name");
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL,"family_name_and_last_name");
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,"family_name_and_last_name");
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN,"family_name_and_last_name");

family_name_and_last_name
FAMILY_NAME_AND_LAST_NAME
familyNameAndLastName
FamilyNameAndLastName
family-name-and-last-name
Converter<String, String> converter = CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.UPPER_UNDERSCORE);
String result = converter.convert("family_name_and_last_name");

FAMILY_NAME_AND_LAST_NAME