Convert a String to Title Case in Java

Learn to convert a string to Title Case format in Java. We will be using the Apache commons’ WordUtils.capitalizeFully() API and manually splitting and appending the words.

1. Using WordUtils

This is the simplest and straightforward solution. Apache commons-text provides WordUtils class that contains utility methods to perform operations on the words of a Java String.

1.1. Maven dependency

Start will the including the latest version of library in the project.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.4</version>
</dependency>

1.2. Using WordUtils.capitalizeFully() for Title Case Conversion

The capitalizeFully() method converts all the whitespace separated words in a String into capitalized words. In the result string, each word is made up of a titlecase character and then a series of lowercase characters.

null input String returns null.

String string = "how TO Do iN JAVA";

String capitalizedString = WordUtils.capitalizeFully(string);

Assertions.assertEquals("How To Do In Java", capitalizedString);

1.3. Capitalize a Delimited String

If the String contains few delimiters that we want for splitting the words, we can use pass the delimiters as the second argument to the method.

Note that capitalizeFully() does not remove the delimiters from the result string, it only uses the delimiters to tokenize the string.

String string = "how-TO Do$iN JAVA";

String capitalizedString = WordUtils.capitalizeFully(string, new char[]{' ', '-', '$'});

Assertions.assertEquals("How-To Do$In Java", capitalizedString);

2. Using String.split() and StringBuffer

Another solution to capitalize a String is manually splitting the string using using space delimiter (or other delimiters) and append the individual tokens after capitalization. The process to capitalize a token is as follows:

  • Convert the first character to uppercase
  • Convert the rest of the string to lowercase
  • Append the result to StringBuffer followed by space(” “) or delimiter
  • Return the result string
public static String titleCase(String inputString) {

  if (StringUtils.isBlank(inputString)) {
    return "";
  }

  if (StringUtils.length(inputString) == 1) {
    return inputString.toUpperCase();
  }

  StringBuffer resultPlaceHolder = new StringBuffer(inputString.length());

  Stream.of(inputString.split(" ")).forEach(stringPart -> {
    
    if (stringPart.length() > 1) {
      resultPlaceHolder.append(stringPart.substring(0, 1).toUpperCase())
          .append(stringPart.substring(1).toLowerCase());
    } else {
      resultPlaceHolder.append(stringPart.toUpperCase());
    }

    resultPlaceHolder.append(" ");
  });
  return StringUtils.trim(resultPlaceHolder.toString());
}

Let us test the titleCase() function with few strings:

Assertions.assertEquals("Null", titleCase(null));
Assertions.assertEquals("", titleCase(""));
Assertions.assertEquals("How To Do In Java", titleCase("HOW to DO IN jAVA"));
Assertions.assertEquals("How To Do In Java", titleCase("how to do in java"));

3. Conclusion

In this short Java string tutorial, we learned to convert the words of a String to titlecase using the WordUtils class and creating custom solution by manually splitting and capitalizing each word separately.

Happy Learning!!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode