In this tutorial, you shall learn how to reverse a given string in Kotlin, using String.reversed() function, with examples.

Kotlin – Reverse a String

To reverse a given string in Kotlin, call reversed() method on this String. reversed() returns a new String with the characters of the original string reversed in order.

Example

In the following example, we take a string in str, and reverse this string using reversed() method.

Main.kt

fun main(args: Array<String>) {
    var str = "hello world"
    var result = str.reversed()
    println(result)
}

Output

dlrow olleh
ADVERTISEMENT

Conclusion

In this Kotlin Tutorial, we learned how to reverse a string using String.reversed() method, with the help of Kotlin example programs.