Convert String to title case in Java using Streams


Title case is capitalized first character of each word. That means, we have to do this:

  • Break at word boundaries
  • Capitalize the first character and lowercase the others
  • Join again at word boundaries.
public String toTitleCase(String str) {
    final String wordBoundary = " ";
            .map(it -> it.substring(0, 1).toUpperCase() + it.substring(1, it.length()).toLowerCase())

Let’s see it in actions:

Roughing It => Roughing It
PRIde AnD PrejudICE => Pride And Prejudice
a tale of two Cities => A Tale Of Two Cities