驼峰式大小写有两种类型:
- 驼峰小写,第一个单词的第一个字符是小写:thisIsLowerCamelCase
- 驼峰大写,也称为标题大写,其中第一个单词的第一个字符是大写的:ThisIsLowerCamelCase
使用正则:
String[] words = text.split("[\\W_]+"); |
StringBuilder builder = new StringBuilder(); |
使用Apache Commons Text,我们需要将它添加到我们的项目中:
<dependency> |
这个库在CaseUtils 中提供了一个toCamelCase方法 :
String camelCase = CaseUtils.toCamelCase(text, false, delimiter); |
使用 Guava,让我们将其依赖项添加到我们的项目中:
<dependency> |
Guava 有一个实用程序类CaseFormat,用于格式转换:
String camelCase = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "THIS_STRING_SHOULD_BE_IN_CAMEL_CASE"); |
完整的源代码可以在 GitHub 上找到