优米格
分享有营养的

Java中将字符串转为驼峰格式

BraUndress.png

方式1

使用CaseUtils 对Java字符串进行转换为驼峰格式:

 CaseUtils.toCamelCase(null, false)                                 = null
 CaseUtils.toCamelCase("", false, *)                                = ""
 CaseUtils.toCamelCase(*, false, null)                              = *
 CaseUtils.toCamelCase(*, true, new char[0])                        = *
 CaseUtils.toCamelCase("To.Camel.Case", false, new char[]{'.'})     = "toCamelCase"
 CaseUtils.toCamelCase(" to @ Camel case", true, new char[]{'@'})   = "ToCamelCase"
 CaseUtils.toCamelCase(" @to @ Camel case", false, new char[]{'@'}) = "toCamelCase"

构造方法:

public static String toCamelCase(String str,
                                 boolean capitalizeFirstLetter,
                                 char... delimiters)

其中:str为要转换的字符串;capitalizeFirstLetter表示是否首字母大写;delimiters指定连词符。

maven依赖:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.8</version>
</dependency>

方式2

使用Google Guavacom.google.common.base.CaseFormat

引入依赖:

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

使用:

CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, string_to_format)
赞(1)
未经允许禁止转载:优米格 » Java中将字符串转为驼峰格式

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系