Convert String to title case in Java example shows how to convert string to title case in Java using various approaches to convert string to camel case or proper case.
How to convert String to title case in Java?
There are various approaches to convert as given below.
1) Convert string to title case using the split and substring methods
Split the String by space, for each part make the first letter uppercase and rest characters to lower case, append the part to the StringBuilder (or StringBuffer) object followed by a space. Finally, convert the StringBuilder to String object and trim the last unwanted space.
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 |
package com.javacodeexamples.stringexamples; public class StringTitleCaseExample { public static void main(String[] args) { String[] strArray = { "hello world", "java code examples", null, "a", "", "TITLE CASE EXAMPLE", }; for(String str : strArray) System.out.println( toTitleCase(str) ); } private static String toTitleCase(String str) { if(str == null || str.isEmpty()) return ""; if(str.length() == 1) return str.toUpperCase(); //split the string by space String[] parts = str.split(" "); StringBuilder sb = new StringBuilder( str.length() ); for(String part : parts){ if(part.length() > 1 ) sb.append( part.substring(0, 1).toUpperCase() ) .append( part.substring(1).toLowerCase() ); else sb.append(part.toUpperCase()); sb.append(" "); } return sb.toString().trim(); } } |
Output
1 2 3 4 5 6 |
Hello World Java Code Examples A Title Case Example |
2) Using the char array and Character class
Instead of the substring
method, you can first convert each part of the string to lower case, create a character array from the lower case part, make the first character upper case and append the character array to 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 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.javacodeexamples.stringexamples; public class StringTitleCaseExample { public static void main(String[] args) { String[] strArray = { "hello world", "java code examples", null, "a", "", "TITLE CASE EXAMPLE", }; for(String str : strArray) System.out.println( toTitleCase(str) ); } private static String toTitleCase(String str) { if(str == null || str.isEmpty()) return ""; if(str.length() == 1) return str.toUpperCase(); String[] parts = str.split(" "); StringBuilder sb = new StringBuilder( str.length() ); for(String part : parts){ char[] charArray = part.toLowerCase().toCharArray(); charArray[0] = Character.toUpperCase( charArray[0] ); sb.append( new String(charArray) ).append(" "); } return sb.toString().trim(); } } |
Output
1 2 3 4 5 6 |
Hello World Java Code Examples A Title Case Example |
3) Using the Apache Commons library
If you are using the Apache Commons library, you can use capitalizeFully
method of WordUtils class to convert the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.text.WordUtils; public class StringTitleCaseExample { public static void main(String[] args) { String[] strArray = { "hello world", "java code examples", null, "a", "", "TITLE CASE EXAMPLE", }; for(String str : strArray) System.out.println( WordUtils.capitalizeFully(str) ); } } |
Output
1 2 3 4 5 6 |
Hello World Java Code Examples null A Title Case Example |
Note: If you are using Java 1.4 or lower version, use StringBuffer instead of the StringBuilder class.
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.
I also want to get the courses via email.
Hi,
Currently, I do not offer any courses via email, but you can bookmark the site to quickly return to it, I would also ask you to like the Facebook page where I post different examples/tutorials daily.