Java String Handling

Capitalize first character of each word of String in Java example

This Java example shows how to capitalize first character of each word of string or make first letter uppercase in Java. Example also shows how to capitalize first character of each word of String using various approaches.

How to capitalize first character of each word of String in Java?

To capitalize 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 String class

You can use substring, split and toUpperCase methods of String class to capitalize first letter of each word of String as given below.


The above program worked for our sample string. Let’s try some other strings as well.



As you can see from the output, our program only capitalized words which are separated by space. It ignores all other punctuation marks like comma (,), full stop (.), quotation marks (single quotes and double quotes), hyphen etc.

In order to include all these, we need to change our program a bit. We need to change the split method to split the string by words using regular expression. We will use \\W pattern which denotes word boundaries.

Here is the updated capitalizeEachWord method.



I have tested this solution with 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 first character of String using Apache Commons

If you don’t want to implement your own solution, you can always use ever reliable Apache Commons library. Use capitalize or capitalizeFully method of WordUtils class as given below.


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

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *