Java String: Title Case Format

Java String: Title Case Format

Learn How to Convert Java String to Title Case Format

1. Introduction

In this short tutorial, we'll learn how to convert a Java String to Title Case format.

2. Solution

The approach I used was to lower-case the String, split the String, title-case each word in the array, then join all the words back together as one String.

Here is the code example:

public String titleCase(String line) {
  return Arrays
    // Lower-case the String.
    // Split the String into an Array.
    .stream(line.toLowerCase().split(" "))
    .map(word -> word.isEmpty()
         ? word
         // Title Case the first letter in each word.
         : Character.toTitleCase(word.charAt(0)) + word
         .substring(1))
    // Join the words back together as one String.
    .collect(Collectors.joining(" "));
}

3. Conclusion

In this short tutorial (maybe the shortest on the web), you learned how to convert a Java String to Title Case format.

Did you like it? Why don't you try also...

Change the Server Port that a Spring Boot App is running on

Change the Server Port that a Spring Boot App is running on

Learn how to change the server port that a spring boot application is running on.

Fill Java Array With The Same Values

Fill Java Array With The Same Values

Learn How To Fill a Java Array With All The Same Values.

Java How to Remove the First Character from a String.

Java How to Remove the First Character from a String.

Learn How to Remove the First Character From a String.