In this quick tutorial, you will learn how to capitalize the first letter of a string in Java. Unfortunately, the String class in Java does not provide any method to capitalize the string. However, there are methods available to change the case of the string (uppercase, lowercase, etc.).

Using String.substring() Method

The simplest way to capitalize the first letter of a string in Java is by using the String.substring() method:

String str = "hello world!";

// capitalize first letter
String output = str.substring(0, 1).toUpperCase() + str.substring(1);

// print the string
System.out.println(output);
// Hello world!

The above example transforms the first letter of string str to uppercase and leaves the rest of the string same. If the string is null or empty, the above code throws an exception.

However, we can write a functional capitalize() that makes sure the string has at least one character before using the substring() method:

public static String capitalize(String str) {
    if(str == null || str.isEmpty()) {
        return str;
    }

    return str.substring(0, 1).toUpperCase() + str.substring(1);
}

Now just call the capitalize() method whenever you want to make the first letter of a string uppercase:

System.out.println(capitalize("hello world!")); // Hello world!
System.out.println(capitalize("heLLo")); // HeLLo
System.out.println(capitalize(null)); // null

Notice the 2nd line of the above code snippet, only the first letter is capitalized. If you want to ensure only the first letter is in uppercase and the remaining string is lowered case, you can do the following:

str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase()

Using Apache Commons Lang

The Apache Commons Lang library is another way to capitalize the first letter of a string in Java. If you are using Gradle, add the following dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:3.9'

For Maven, add the dependency below to your pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

The StringUtils class from Commons Lang provides the capitalize() method to change the first letter to uppercase:

System.out.println(StringUtils.capitalize("apache commons")); // Apache commons
System.out.println(StringUtils.capitalize("heLLO")); // HeLLO
System.out.println(StringUtils.uncapitalize(null)); // null

Apache Commons Lang offers many extra methods that are not included in standard Java libraries (java.lang package). It includes String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties.

Read Next: Capitalize the first letter of each word in a string using Java

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.