Kotlin – Capitalize String

Capitalize a String in Kotlin

To capitalize a given string in Kotlin, you can use String.capitalize() function. The function capitalises the first character in the string.

Call capitalize() function on the given String object str.

str.capitalize()

capitalize() function returns a new string created by capitalizing the first character in the given string.

Examples

Capitalize the string “hello world”

In the following program, we take a string value in str, and capitalize this string.

Kotlin Program

fun main() {
    val str = "hello world"
    val resultString = str.capitalize()
    println(resultString)
}

Output

Hello world