Kotlin – Convert String to Uppercase

Convert String to Uppercase in Kotlin

To convert a given string to an uppercase string in Kotlin, you can use String.toUpperCase() function.

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

str.toUpperCase()

toUpperCase() function returns a new string created from converting all the characters of the calling string to uppercase.

Examples

Convert given string to uppercase

Consider a scenario where we are given a string, say "Hello World" in a variable str. We need to convert the characters in this string to their uppercase, and print the resulting string.

Kotlin Program

fun main() {
    val str = "Hello World"
    val uppercaseString = str.toUpperCase()
    println(uppercaseString)
}

Output

HELLO WORLD