This example will show how to capitalize every word in a sentence using java and apache commons. Similar to capitalizing the first letter in a sentence we will use another phrase from the famous Super why! theme song.
Straight up Java
This snippet will show how to capitalize the first char of each word in a sentence using core java. Assuming that your phrase is separated by a space, this will split the sentence into an array and capitalize the first letter of the word. There is definitely gaps in this approach as you will need to handle multiple spaces between words and removing the space at the end.
Capitalize each word
Capitalize each word w/ breakiterator
This snippet will capitalize the first letter of each word using BreakIterator. This feels a bit complicated and more code but it does have the opportunity to handle specific use cases. The BreakIterator implements methods to find the location and boundaries of text maintaining the current position. The first method below will loop through each position of the sentence and call the nextWordStartAfter. The nextWordStartAfter will find the next word in the sentence while checking if there is any other words to process. The main method will then upper case the first letter of the word.
The WordUtils.capitalizeFully method in apache commons will convert all the whitespace separated words in a String into capitalized words.