This Java example shows how to capitalize first character of each word of string or make first letter uppercase in Java. The example also shows how to capitalize the first character of each word of String using various approaches.
How to capitalize the first character of each word of String in Java?
To capitalize the first character of each word of String, you can use one of the below given two approaches.
1) Using substring, toUpperCase, and split methods of the String class
We can use the substring, split, and toUpperCase methods of the String class to capitalize the first letter of each word of String as given 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.javacodeexamples.stringexamples; public class CapitalizeFirstCharEachWordExample { public static void main(String[] args) { String str = "this is a test to capitalize each word in string"; System.out.println(capitalizeEachWord(str)); } private static String capitalizeEachWord(String str){ //if string is null or empty, return empty string if(str == null || str.length() == 0) return ""; /* * if string contains only one char, * make it capital and return */ if(str.length() == 1) return str.toUpperCase(); /* * Split the string by space */ String[] words = str.split(" "); //create empty StringBuilder with same length as string StringBuilder sbCapitalizedWords = new StringBuilder(str.length()); /* * For each word, * 1. get first character using substring * 2. Make it upper case and append to string builder * 3. append the rest of the characters as-is to string builder * 4. append space to string builder */ for(String word : words){ if(word.length() > 1) sbCapitalizedWords .append(word.substring(0, 1).toUpperCase()) .append(word.substring(1)); else sbCapitalizedWords.append(word.toUpperCase()); sbCapitalizedWords.append(" "); } /* * convert StringBuilder to string, also * remove last space from it using trim method */ return sbCapitalizedWords.toString().trim(); } } |
Output
1 |
This Is A Test To Capitalize Each Word In String |
The above program worked for our sample string. Let’s try some other strings as well.
Inputs
1 2 3 |
I love Java.i also like c++,and c i love programming:java programming i like when someone says 'i love programming' |
Outputs
1 2 3 |
I Love Java.i Also Like C++,and C I Love Programming:java Programming I Like When Someone Says 'i Love Programming' |
As you can see from the output, our program only capitalized words that are separated by space. It ignores all other punctuation marks like comma (,), full stop (.), quotation marks (single quotes and double quotes), hyphens, etc.
In order to include all these characters, we need to change our program a bit. We need to change the split method to split the string by words using the regular expression. We will use \\W
pattern which denotes word boundaries.
Here is the updated capitalizeEachWord
method.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 |
private static String capitalizeEachWord(String str){ //if string is null or empty, return empty string if(str == null || str.length() == 0) return ""; /* * if string contains only one char, * make it capital and return */ if(str.length() == 1) return str.toUpperCase(); /* * Split the string by word boundaries */ String[] words = str.split("\\W"); //create empty StringBuilder with same length as string StringBuilder sbCapitalizedWords = new StringBuilder(str.length()); for(String word : words){ if(word.length() > 1) sbCapitalizedWords .append(word.substring(0, 1).toUpperCase()) .append(word.substring(1)); else sbCapitalizedWords.append(word.toUpperCase()); //we do not want to go beyond string length if(sbCapitalizedWords.length() < str.length()){ sbCapitalizedWords.append(str.charAt(sbCapitalizedWords.length())); } } /* * convert StringBuilder to string, also * remove last space from it using trim method, if there is any */ return sbCapitalizedWords.toString().trim(); } |
Inputs
1 2 3 |
I love Java.i also like c++,and c i love programming:java programming i like when someone says 'i love programming' |
Outputs
1 2 3 |
I Love Java.I Also Like C++,And C I Love Programming:Java Programming I Like When Someone Says 'I Love Programming' |
I have tested this solution with a limited number of input strings and it worked as expected. It may need some minor tweaks in case if the above method does not work for any particular use case.
2) Capitalize the first character of String using Apache Commons
If you don’t want to implement your own solution, you can always use the ever-reliable Apache Commons library. Use the capitalize
or capitalizeFully
method of the WordUtils class as given below.
1 2 3 4 5 |
public static void main(String[] args) { String str = "This Is A Test To Capitalize Each Word In String"; System.out.println(WordUtils.capitalizeFully(str)); } |
Output
1 |
This Is A Test To Capitalize Each Word In String |
This example is a part of Java String tutorial.
Please let me know your views in the comments section below.