1. Overview
1.概述
Camel case and title case are commonly used as identifiers for fields and types. We may wish to convert text into this format.
Camel大小写和title大小写通常被用作字段和类型的标识。我们可能希望将文本转换为这种格式。
This can be achieved either by writing custom code or by making use of third-party libraries.
这可以通过编写自定义代码或利用第三方库来实现。
In this tutorial, we’ll look at how to write some custom string conversions to camel cases, and we’ll explore some third-party library features that can help us with that task.
在本教程中,我们将看看如何编写一些自定义的字符串转换为骆驼案,我们将探索一些可以帮助我们完成这一任务的第三方库功能。
2. Java Solutions
2.Java解决方案
Camel’s case allows us to join multiple words by removing whitespace and using capital letters to show word boundaries.
Camel’s case允许我们通过去除空白并使用大写字母来显示单词的边界来连接多个单词。
There are two types:
有两种类型。
- Lower camel case, where the first character of the first word is in lowercase
- Upper camel case, also known as title case, where the first character of the first word is in uppercase:
thisIsLowerCamelCase
ThisIsLowerCamelCase
In this tutorial, we’ll focus on conversion to lower camel case, though these techniques are easily adapted to suit either.
在本教程中,我们将重点讨论转换为小写骆驼字母,尽管这些技术很容易适应这两种情况。
2.1. Regular Expression (Regex)
2.1 正则表达式(Regex)
We can use regular expressions to split our string containing words into an array:
我们可以使用regular expressions来将我们包含单词的字符串分割成一个数组。
String[] words = text.split("[\\W_]+");
This splits the given string at any character that’s not part of a word. The underscore is normally considered a word character in regular expressions. Camel’s case does not include underscore, so we’ve added that to the delimiter expression.
这将在任何不属于一个单词的字符处分割给定的字符串。在正则表达式中,下划线通常被认为是一个单词字符。Camel的情况不包括下划线,所以我们把它加到分隔符表达式中。
When we have the separate words, we can modify their capitalization and reassemble them as camel cases:
当我们有了独立的单词,我们可以修改它们的大小写,并将它们重新组合成骆驼的大小写。
StringBuilder builder = new StringBuilder();
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (i == 0) {
word = word.isEmpty() ? word : word.toLowerCase();
} else {
word = word.isEmpty() ? word : Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
}
builder.append(word);
}
return builder.toString();
Here, we convert the first string/word in the array to lowercase. For every other word in the array, we convert the first character to uppercase and the rest to lowercase.
在这里,我们将数组中的第一个字符串/单词转换为小写。对于数组中的其他每个词,我们将第一个字符转换为大写,其余的转换为小写。
Let’s test this method using white space as the non-word characters:
让我们用白色空间作为非字的字符来测试这个方法。
assertThat(toCamelCaseByRegex("THIS STRING SHOULD BE IN CAMEL CASE"))
.isEqualTo("thisStringShouldBeInCamelCase");
This solution is straightforward, but it requires a few copies of the original text in order to calculate the answer. First, it creates a list of the words, then creates copies of those words in various capitalized or lowercase formats to compose the final string. This may consume a lot of memory with very large input.
这个解决方案很直接,但它需要几份原始文本的副本,以便计算出答案。首先,它创建一个单词列表,然后以各种大写或小写格式创建这些单词的副本,以组成最终的字符串。如果输入量非常大,这可能会消耗大量的内存。
2.2. Iterating Through the String
2.2.遍历字符串
We could replace the above algorithm with a loop that works out the correct case of each character as it passes through the original string. This skips any delimiters and writes one character at a time to the StringBuilder.
我们可以用一个循环来代替上面的算法,当它通过原始字符串时,计算出每个字符的正确情况。这将跳过任何定界符,并一次写一个字符到StringBuilder。
First, we need to track the state of the conversion:
首先,我们需要跟踪转换的状态。
boolean shouldConvertNextCharToLower = true;
Then we iterate through the source text, skipping or appropriately capitalizing each character:
然后,我们在源文本中进行迭代,跳过或适当地大写每个字符。
for (int i = 0; i < text.length(); i++) {
char currentChar = text.charAt(i);
if (currentChar == delimiter) {
shouldConvertNextCharToLower = false;
} else if (shouldConvertNextCharToLower) {
builder.append(Character.toLowerCase(currentChar));
} else {
builder.append(Character.toUpperCase(currentChar));
shouldConvertNextCharToLower = true;
}
}
return builder.toString();
The delimiter character here is a char that represents the expected non-word character.
这里的分隔符是一个char,代表预期的非字字符。
Let’s try this solution using space as the delimiter:
让我们试试这个使用空格作为分隔符的解决方案。
assertThat(toCamelCaseByIteration("THIS STRING SHOULD BE IN CAMEL CASE", ' '))
.isEqualTo("thisStringShouldBeInCamelCase");
We can also try it with an underscore delimiter:
我们也可以用下划线分隔符来试试。
assertThat(toCamelCaseByIteration("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", '_'))
.isEqualTo("thisStringShouldBeInCamelCase");
3. Using Third-Party Libraries
3.使用第三方库
We may prefer to use third-party library string functions, rather than write our own.
我们可能更喜欢使用第三方库的字符串函数,而不是自己写。
3.1. Apache Commons Text
3.1.Apache Commons文本
To use Apache Commons Text, we need to add it to our project:
要使用Apache Commons Text,我们需要将其加入我们的项目。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
This library provides a toCamelCase method in CaseUtils:
这个库在CaseUtils中提供了一个toCamelCase方法。
String camelCase = CaseUtils.toCamelCase(text, false, delimiter);
Let’s try it out:
让我们来试一试。
assertThat(CaseUtils.toCamelCase("THIS STRING SHOULD BE IN CAMEL CASE", false, ' '))
.isEqualTo("thisStringShouldBeInCamelCase");
In order to turn the string to title case or upper camel case, we need to pass true into the toCamelCase method:
为了将字符串变成标题大小写或大写驼峰,我们需要将true传入toCamelCase方法。
String camelCase = CaseUtils.toCamelCase(text, true, delimiter);
Let’s try it out:
让我们来试一试。
assertThat(CaseUtils.toCamelCase("THIS STRING SHOULD BE IN CAMEL CASE", true, ' '))
.isEqualTo("ThisStringShouldBeInCamelCase");
3.2. Guava
3.2.番石榴
With a little pre-processing, we can convert a string to camel via Guava.
通过一点预处理,我们可以通过Guava将一个字符串转换为骆驼。
To use Guava, let’s add its dependency to our project:
为了使用Guava,让我们把它的依赖性添加到我们的项目中。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
Guava has a utility class, CaseFormat, for format conversion:
Guava有一个实用类,CaseFormat,用于格式转换。
String camelCase = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "THIS_STRING_SHOULD_BE_IN_CAMEL_CASE");
This converts a given uppercase string separated by underscores to a lower camel case. Let’s see it:
这将给定的由下划线分隔的大写字符串转换为小写的驼峰。让我们来看看。
assertThat(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "THIS_STRING_SHOULD_BE_IN_CAMEL_CASE"))
.isEqualTo("thisStringShouldBeInCamelCase");
This is fine if our string is already in this format. However, if we wish to use a different delimiter and handle mixed cases, we’ll need to pre-process our input:
如果我们的字符串已经是这种格式,这就很好。但是,如果我们希望使用不同的分隔符并处理混合情况,我们就需要对我们的输入进行预处理。
String toUpperUnderscore = "This string should Be in camel Case"
.toUpperCase()
.replaceAll(' ', "_");
First, we convert the given string to uppercase. Then, we replace all the separators with underscores. The resulting format is the equivalent of Guava’s CaseFormat.UPPER_UNDERSCORE. Now we can use Guava to produce the camel case version:
首先,我们将给定的字符串转换成大写字母。然后,我们用下划线替换所有的分隔符。由此产生的格式等同于Guava的CaseFormat.UPPER_UNDERSCORE。现在我们可以使用Guava来产生骆驼字母的版本。
assertThat(toCamelCaseUsingGuava("THIS STRING SHOULD BE IN CAMEL CASE", " "))
.isEqualTo("thisStringShouldBeInCamelCase");
4. Conclusion
4.总结
In this tutorial, we’ve learned how to convert a string to a camel case.
在本教程中,我们已经学会了如何将一个字符串转换为骆驼的大小写。
First, we built an algorithm to split the string into words. Then we built an algorithm that iterated over each character.
首先,我们建立了一个算法,将字符串分割成单词。然后,我们建立了一个算法,对每个字符进行迭代。
Finally, we looked at how to use some third-party libraries to achieve the result. Apache Commons Text was a close match, and Guava could help us after some pre-processing.
最后,我们研究了如何使用一些第三方库来实现这个结果。Apache Commons Text是一个近似的选择,而Guava在经过一些预处理后可以帮助我们。
As usual, the complete source code is available over on GitHub.
像往常一样,完整的源代码可以在GitHub上找到,。