Capitalize first letter of a String in Java
This post will discuss how to capitalize the first letter of a String in Java.
1. Using String.toUpperCase() method
The idea is to extract the first character from the string and convert it to uppercase by calling the toUpperCase() method. Once we have converted the first character to uppercase, use the string concatenation operator to concatenate it with the remaining string.
Note that since the String is immutable in Java, no modifications can be done to the actual string. The solution creates a new string instance. The following code demonstrates this using the substring() method.
|
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { String str = "capitalize me"; str = str.substring(0, 1).toUpperCase() + str.substring(1); System.out.println(str); } } |
Output:
Capitalize me
If the string is empty, then the above program throws a StringIndexOutOfBoundsException. If the string is null, NullPointerException is thrown. The following program creates a utility method to capitalize a string that handles both these exceptions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Main { public static String capitalize(String str) { if (str == null || str.length() == 0) { return str; } return str.substring(0, 1).toUpperCase() + str.substring(1); } public static void main(String[] args) { String str = "capitalize me"; System.out.println(capitalize(str)); } } |
Output:
Capitalize me
2. Using StringUtils.capitalize() method
Another solution is to use the StringUtils class from Apache Commons Lang library. It already provides a capitalize() method that serves the same purpose.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.lang3.StringUtils; public class Main { public static void main(String[] args) { String str = "capitalize me"; System.out.println(StringUtils.capitalize(str)); } } |
Output:
Capitalize me
2. Using WordUtils.capitalize() method
If you need to capitalize the first character of each word in a string, you should use the capitalize() method from the WordUtils class.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.text.WordUtils; public class Main { public static void main(String[] args) { String str = "iKEA store"; System.out.println(WordUtils.capitalize(str)); } } |
Output:
IKEA Store
If you need to capitalize the first character and convert the remaining characters of each word to lowercase, use the capitalizeFully() method instead.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.text.WordUtils; public class Main { public static void main(String[] args) { String str = "iKEA store"; System.out.println(WordUtils.capitalizeFully(str)); } } |
Output:
Ikea Store
4. Using Stream API
Alternatively, with Java 8, you can get a stream of all whitespace-separated words, and capitalize each word using the same approach discussed before in this post. The following solution demonstrates this.
|
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 |
import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static String capitalizeWord(String word) { if (word == null || word.length() == 0) { return word; } return word.substring(0, 1).toUpperCase() + word.substring(1); } public static String capitalize(String str) { if (str == null || str.length() == 0) { return str; } return Stream.of(str.trim().split("\\s+")) .map(Main::capitalizeWord) .collect(Collectors.joining(" ")); } public static void main(String[] args) { String str = "capitalize me"; System.out.println(capitalize(str)); } } |
Output:
Capitalize Me
That’s all about capitalizing the first letter of a String in Java.
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 :)