To reverse a string in Kotlin, you can use the built-in extension function reversed()
.
In this example,
str1
with a value of "Hello World"
.reversed()
extension function on str1
.fun main() {
val str1 = "Hello World"
val reversedStr = str1.reversed()
println("Reversed string is: $reversedStr")
}
Reversed string is: dlroW olleH
In this example,
str2
with a value of "apple"
.reversed()
extension function on str2
.fun main() {
val str2 = "apple"
val reversedStr = str2.reversed()
println("Reversed string is: $reversedStr")
}
Reversed string is: elppa
In this tutorial, we learned How to Reverse a String in Kotlin language with well detailed examples.