This Java example shows how to capitalize first character of string or make first letter uppercase in Java using various approaches.
How to capitalize first character or make first letter uppercase?
To capitalize the first character of String or make first letter uppercase, you can use one of the below given approaches.
1) Capitalize the first character of String using the substring and toUpperCase methods of the String class
You can use the substring
and toUpperCase
methods of the String class to capitalize the first letter 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 |
package com.javacodeexamples.stringexamples; public class CapitalizeFirstCharStringExample { public static void main(String[] args) { String str = "string capitalize example"; System.out.println( capitalize(str) ); } private static String capitalize(String str){ if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toUpperCase(); return str.substring(0, 1).toUpperCase() + str.substring(1); } } |
Output
1 |
String capitalize example |
If you want to convert the first character of a string to upper case and all other characters to lower case, change the method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private static String capitalize(String str){ if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toUpperCase(); return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase(); } } |
2) Using the Apache Commons
If you are using the Apache Commons library, you can use the capitalize
method of the StringUitls class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class CapitalizeFirstCharStringExample { public static void main(String[] args) { String str = "string capitalize example"; System.out.println( StringUtils.capitalize(str) ); } } |
Output
1 |
String capitalize example |
3) Using the charAt method of String class and the toUpperCase method of the Character class
You can also use the charAt
method of the String class and the toUpperCase
method of the Character class to capitalize the first character of a 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 |
package com.javacodeexamples.stringexamples; public class CapitalizeFirstCharStringExample { public static void main(String[] args) { String str = "string capitalize example"; System.out.println( capitalize(str) ); } private static String capitalize(String str){ if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toUpperCase(); return Character.toUpperCase(str.charAt(0)) + str.substring(1); } } |
Output
1 |
String capitalize example |
4) Using a character array
This approach converts string to char array, make the first element of an array to uppercase and then converts the character array back to the 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 |
package com.javacodeexamples.stringexamples; public class CapitalizeFirstCharStringExample { public static void main(String[] args) { String str = "string capitalize example"; System.out.println( capitalize(str) ); } private static String capitalize(String str){ if(str == null || str.length() == 0) return ""; if(str.length() == 1) return str.toUpperCase(); char[] charArray = str.toCharArray(); charArray[0] = Character.toUpperCase(charArray[0]); return new String(charArray); } } |
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.