This article explores different ways to capitalize the first letter of a String in Kotlin.

1. Capitalize first letter

The simplest solution is to use the toUpperCase() function. It returns a copy of the string with its first letter upper-cased. If the string is empty or already starts with an upper case letter, it returns the original string.

Download Code

 
Since String is immutable in Kotlin, no modifications are possible in the original string. If the string can be null, use the Elvis operator with null check:

Download Code

2. Capitalize each word

The idea is to get a list of all whitespace-separated words, and capitalize each word using the toUpperCase() function. The following solution demonstrates this.

Download Code

 
We can merge the call chain to joinToString() with a transform function:

Download Code

That’s all about capitalizing the first letter of a String in Kotlin.