Java String Handling

Convert String to title case in Java example

Convert String to title case in Java example shows how to convert string to title case in Java. Example also shows various approaches to convert string to title case (camel case/proper case).

How to convert String to title case in Java?

There are various approaches to convert string to title case as given below.

1) Convert String to title case using split and substring methods

Split the String by space, for each part make the first letter uppercase and rest lower case, append the part to the StringBuilder followed by a space. Finally, convert the StringBuilder to String object and trim the last unwanted space.

Output

2) Convert String to title case using char array and Character class

Instead of substring method, you can first convert each part of the string to lower case, create a character array from the lowercase part, make the first character upper case and append the character array to the StringBuilder.

Output

3) Convert String to title case using Apache Commons library

If you are using Apache Commons library, you can use capitalizeFully method of WordUtils class to convert String to title case as given below.

Output

Note: If you are using Java 1.4 or lower version, use StringBuffer instead of StringBuilder class.

Please let us know your views in the comments section below.

 

Join 1000+ fellow learners! Enter your email address below: