Capitalize the first letter of a string in Java

In this quick tutorial, you will learn how to capitalize the first letter of a string in Java. Unfortunately, the String class does not provide any method to capitalize strings. 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!"

;

String

output

=

str

.

substring

(

0

,

1

)

.

toUpperCase

(

)

+

str

.

substring

(

1

)

;

System

.

out

.

println

(

output

)

;

The above example transforms the first letter of string str to uppercase and leaves the rest of the string the 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!"

)

)

;

System

.

out

.

println

(

capitalize

(

"heLLo"

)

)

;

System

.

out

.

println

(

capitalize

(

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"

)

)

;

System

.

out

.

println

(

StringUtils

.

capitalize

(

"heLLO"

)

)

;

System

.

out

.

println

(

StringUtils

.

uncapitalize

(

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.

Conclusion

In this article, we looked at different ways to capitalize the first letter of a string in Java. The simple approach is to use the String class’s substring() method. However, if you are already using Apache Commons Lang in your project, just use the StringUtils class to capitalize the first letter of a string.

Since string capitalization is a common task in all programming languages, I have also written a tutorial to do the same in JavaScript. Take a look at this guide.

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

✌️ Like this article? Follow me on
Twitter
and LinkedIn.
You can also subscribe to
RSS Feed.