Kotlin – Reverse String

Reverse a String in Kotlin

To reverse a given string in Kotlin, you can use String.reversed() function. The function reverses the order of characters in the string.

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

str.reversed()

reversed() function returns a new string created by reversing the given string.

Examples

Reverse the string “Hello World”

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

Kotlin Program

fun main() {
    val str = "Hello World"
    val reversedString = str.reversed()
    println(reversedString)
}

Output

dlroW olleH