Capitalize First Character Of Each Word In A String In Java
This post will discuss how to capitalize the first character of each word in a given text in Java.
1. Naive solution
A naive solution is to split the given text using space as a delimiter. Then we iterate through the word array, capitalize the first character of each word, and append it to a StringBuilder along with space. Finally, return the string representation of the StringBuilder.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Java program to capitalize the first character of each word in a string class Main { public static String capitalize(String str) { String[] words = str.split("\\s"); StringBuilder sb = new StringBuilder(); for (String s: words) { if (!s.equals("")) { sb.append(Character.toUpperCase(s.charAt(0))); sb.append(s.substring(1)); } sb.append(" "); } // `trim()` to remove extra space at the end before returning return sb.toString().trim(); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = capitalize(sentence); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
2. Using WordUtils from Apache Commons
The simplest solution is to use the WordUtils class from Apache Commons Lang. It already provides a capitalize() method that serves the same purpose.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.apache.commons.lang3.text.WordUtils; class Main { public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = WordUtils.capitalize(sentence); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
3. Using Java 8
In Java 8, we can get the stream of the words in the given text, capitalize the first character of each word, and collect it using Collectors.
⮚ Using Pattern.splitAsStream() to get Stream
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.regex.Pattern; import java.util.stream.Collectors; class Main { public static String capitalize(String s) { if (s.equals("")) { return s; } return s.substring(0, 1).toUpperCase() + s.substring(1); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = Pattern.compile("\\s") .splitAsStream(sentence) .map(Main::capitalize) .collect(Collectors.joining(" ")); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
⮚ Using Stream.of() to get Stream
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static String capitalize(String s) { if (s.equals("")) { return s; } return s.substring(0, 1).toUpperCase() + s.substring(1); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = Stream.of(sentence.split("\\s")) .map(Main::capitalize) .collect(Collectors.joining(" ")); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.StringJoiner; class Main { public static String capitalize(String s) { if (s.equals("")) { return s; } return s.substring(0, 1).toUpperCase() + s.substring(1); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; StringJoiner joiner = new StringJoiner(" "); for (String s : sentence.split("\\s")) { joiner.add(capitalize(s)); } String str = joiner.toString(); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
4. Using Guava Library
We can also use the Joiner class from Guava, along with Splitter and Iterables class, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import org.apache.commons.lang3.StringUtils; class Main { public static String capitalize(String s) { if (s.equals("")) { return s; } return s.substring(0, 1).toUpperCase() + s.substring(1); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = Joiner.on(' ') .join(Iterables.transform(Splitter.on(' ').split(sentence), Main::capitalize)); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
5. Using Regex
We can also take the help of regular expressions in Java to capitalize the first character of each word, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.regex.Matcher; import java.util.regex.Pattern; class Main { public static String capitalize(String str) { StringBuffer sb = new StringBuffer(); Matcher matcher = Pattern.compile("\\b(\\w){1}").matcher(str); while (matcher.find()) { matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); } matcher.appendTail(sb); return sb.toString(); } public static void main(String[] args) { String sentence = "techie delight is awesome!"; String str = capitalize(sentence); System.out.println(str); } } |
Output:
Techie Delight Is Awesome!
That’s all about capitalizing the first character of each word in a Java String.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)