1. Introduction
1.介绍
In this short tutorial, we’ll show how to convert a String to title case format in Java.
在这个简短的教程中,我们将展示如何在Java中把一个String转换成标题大小写格式。
We’ll show different ways of implementing a custom method and we’ll also show how to do it using third party libraries.
我们将展示实现自定义方法的不同方式,我们还将展示如何使用第三方库来实现。
2. Core Java Solutions
2.核心Java解决方案
2.1. Iterating Through the String Characters
2.1.通过字符串字符进行迭代
One way to convert a String to title case is by iterating through all the characters of the String.
将String转换为标题大小写的一种方法是通过迭代String的所有字符。
To do so, when we find a word separator we capitalize the next character. After that, we change the rest of the characters to lower case until we reach the next word separator.
为此,当我们找到一个词的分隔符时,我们将下一个字符大写。之后,我们将其余的字符改为小写,直到我们到达下一个单词分隔符。
Let’s use a white space as a word separator and implement this solution:
让我们用一个空白作为单词分隔符,实现这个解决方案。
public static String convertToTitleCaseIteratingChars(String text) {
if (text == null || text.isEmpty()) {
return text;
}
StringBuilder converted = new StringBuilder();
boolean convertNext = true;
for (char ch : text.toCharArray()) {
if (Character.isSpaceChar(ch)) {
convertNext = true;
} else if (convertNext) {
ch = Character.toTitleCase(ch);
convertNext = false;
} else {
ch = Character.toLowerCase(ch);
}
converted.append(ch);
}
return converted.toString();
}
As we can see, we use the method Character.toTitleCase to do the conversion, since it checks the title case equivalent of a Character in Unicode.
正如我们所看到的,我们使用Character.toTitleCase方法来进行转换,因为它检查Unicode中Character的标题大小写等效。
If we test this method using these inputs:
如果我们用这些输入来测试这个方法。
tHis IS a tiTLe
tHis, IS a tiTLe
We get the following expected outputs:
我们得到以下预期产出。
This Is A Title
This, Is A Title
2.2. Splitting Into Words
2.2.分裂成词
Another approach to do this is to split the String into words, convert every word to title case, and finally join all the words again using the same word separator.
另一种方法是将String分割成单词,将每个单词转换为标题大小写,最后使用相同的单词分隔符再次连接所有单词。
Let’s see it in code, using again the white space as a word separator, and the helpful Stream API:
让我们看看它的代码,再次使用空白处作为单词分隔符,以及有用的Stream API。
private static final String WORD_SEPARATOR = " ";
public static String convertToTitleCaseSplitting(String text) {
if (text == null || text.isEmpty()) {
return text;
}
return Arrays
.stream(text.split(WORD_SEPARATOR))
.map(word -> word.isEmpty()
? word
: Character.toTitleCase(word.charAt(0)) + word
.substring(1)
.toLowerCase())
.collect(Collectors.joining(WORD_SEPARATOR));
}
Using the same inputs as before, we get the exact same outputs:
使用与之前相同的输入,我们得到完全相同的输出。
This Is A Title
This, Is A Title
3. Using Apache Commons
3.使用Apache Commons
In case we don’t want to implement our own custom method, we can use the Apache Commons library. The setup for this library is explained in this article.
如果我们不想实现自己的自定义方法,我们可以使用Apache Commons库。这个库的设置在这篇文章中解释。
This provides the WordUtils class, that has the capitalizeFully() method which does exactly what we want to achieve:
这提供了WordUtils类,它有capitalizeFully()方法,该方法正是我们想要实现的。
public static String convertToTileCaseWordUtilsFull(String text) {
return WordUtils.capitalizeFully(text);
}
As we can see, this is very easy to use and if we test it with the same inputs as before we get the same results:
正如我们所看到的,这非常容易使用,如果我们用与之前相同的输入进行测试,我们会得到相同的结果。
This Is A Title
This, Is A Title
Also, the WordUtils class provides another capitalize() method that works similarly to capitalizeFully(), except it only changes the first character of each word. This means that it doesn’t convert the rest of the characters to lower case.
另外,WordUtils类提供了另一个capitalize()方法,其工作原理与capitalizeFully()类似,但它只改变每个单词的第一个字符。这意味着它不会将其余的字符转换为小写。
Let’s see how we can use this:
让我们看看我们如何使用这个。
public static String convertToTileCaseWordUtils(String text) {
return WordUtils.capitalize(text);
}
Now, if we test it with the same inputs as before we get these different outputs:
现在,如果我们用与之前相同的输入来测试它,我们会得到这些不同的输出。
THis IS A TiTLe
THis, IS A TiTLe
4. Using ICU4J
4.使用ICU4J
Another library that we can use is ICU4J, which provides Unicode and globalization support.
我们可以使用的另一个库是ICU4J,它提供Unicode和全球化支持。
To use it we need to add this dependency to our project:
为了使用它,我们需要将这个依赖关系添加到我们的项目中。
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>61.1</version>
</dependency>
The latest version can be found here.
最新版本可以在这里找到。
This library works in a very similar way as WordUtils, but we can specify a BreakIterator to tell the method how we want to split the String, and therefore what words we want to convert to title case:
这个库的工作方式与WordUtils非常相似,但我们可以指定一个BreakIterator来告诉方法我们要如何分割String,因此我们要将哪些词转换为标题大小写。
public static String convertToTitleCaseIcu4j(String text) {
if (text == null || text.isEmpty()) {
return text;
}
return UCharacter.toTitleCase(text, BreakIterator.getTitleInstance());
}
As we can see, they have a specific BreakIterator to work with titles. If we don’t specify any BreakIterator it uses the defaults from Unicode, which in this case generate the same results.
我们可以看到,他们有一个特定的BreakIterator来处理标题。如果我们不指定任何BreakIterator,它就会使用Unicode的默认值,在这种情况下产生相同的结果。
Also, notice that this method lets us specify the Locale of the String we are converting in order to do a locale-specific conversion.
另外,注意这个方法可以让我们指定我们要转换的字符串的Locale,以便做一个特定于当地的转换。
5. Conclusion
5.结论
In this brief article, we’ve shown how to convert a String to title case format in Java. We’ve used our custom implementations first, and after that, we’ve shown how to do it using external libraries.
在这篇简短的文章中,我们展示了如何在Java中把一个String转换为标题大小写格式。我们首先使用了我们的自定义实现,之后,我们展示了如何使用外部库来实现。
As always, the full source code of the examples is available over on GitHub.
一如既往,这些示例的完整源代码可在GitHub上获得。