Convert String to Title Case in Java

Java examples to convert a string to title case using Apache commons WordUtils class, String.split() method and Java 8 stream of character array.

1. Convert String to Title Case – WordUtils

Apache commons-text provides WordUtils to perform operations on String that contains words.

1.1. Maven dependency

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

1.2. WordUtils example to convert string to title case

public class StringExample 
{
    public static void main(String[] args) 
    {

        final char[] delimiters = { ' ', '_' };

        WordUtils.capitalizeFully(null, delimiters);
        WordUtils.capitalizeFully(" ", delimiters);
        WordUtils.capitalizeFully("a", delimiters);
        WordUtils.capitalizeFully("thor almighty", delimiters);
        WordUtils.capitalizeFully("string operation", delimiters);
        WordUtils.capitalizeFully("TITLE CASE CONVERSION", delimiters);
    }
}

Program output:

null
A
Thor Almighty
String Operation
Title Case Conversion

2. Convert string to title case – String.split() and StringBuffer

To convert a string to title case in Java with String.split() method, use following logic:

  • Convert the first character to uppercase
  • Convert rest of the string to lowercase
  • Append the result to StringBuffer followed by space(” “)
  • Return the result string
public class StringExample 
{
    public static void main(String[] args) 
    {
    	titleCaseConversion(null);
    	titleCaseConversion(" ");
    	titleCaseConversion("a");
    	titleCaseConversion("thor almighty");
    	titleCaseConversion("string operation");
    	titleCaseConversion("TITLE CASE CONVERSION");
    }

    private static String titleCaseConversion(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());
    }
}

Program output:

null
A
Thor Almighty
String Operation
Title Case Conversion

3. Convert String to title case – Stream of character array

To convert a string to title case with char array, use following logic:

  • convert the string to lowercase
  • convert the above lowercased string to a character array
  • convert the first character in array to uppercase
  • append the resulting string from character array new String(charArray) to StringBuffer followed by space(” “).
  • return the trimmed result
public class StringExample 
{
    public static void main(String[] args) {

        titleCaseConversion(null);
    	titleCaseConversion(" ");
    	titleCaseConversion("a");
    	titleCaseConversion("thor almighty");
    	titleCaseConversion("string operation");
    	titleCaseConversion("TITLE CASE CONVERSION");
    }

    private static String titleCaseConversion(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 -> {
            char[] charArray = stringPart.toLowerCase().toCharArray();
            charArray[0] = Character.toUpperCase(charArray[0]);
            resultPlaceHolder.append(new String(charArray)).append(" ");
        });

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

Program output:

null
A
Thor Almighty
String Operation
Title Case Conversion

Happy Learning!!

Leave a Reply

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