首页 > 其他分享 >使用google guava类库进行属性名称的驼峰、下划线以及连接符格式互转

使用google guava类库进行属性名称的驼峰、下划线以及连接符格式互转

时间:2022-12-03 23:35:04浏览次数:37  
标签:类库 userName converter System 互转 连接符 println out CaseFormat


添加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();
}

}


标签:类库,userName,converter,System,互转,连接符,println,out,CaseFormat
From: https://blog.51cto.com/u_15891990/5908796

相关文章