To get the last N characters of a given string in Kotlin, you can use the `takeLast` extension function.
In this example,
str1
with a string value of "Hello World"
.takeLast
extension function with str1.takeLast(5)
to get the last 5 characters.fun main() {
val str1 = "Hello World"
val last5Chars = str1.takeLast(5)
println("Last 5 characters of the string are: $last5Chars")
}
Last 5 characters of the string are: World
In this example,
str2
with a string value of "apple"
.takeLast
extension function with str2.takeLast(3)
to get the last 3 characters.fun main() {
val str2 = "apple"
val last3Chars = str2.takeLast(3)
println("Last 3 characters of the string are: $last3Chars")
}
Last 3 characters of the string are: ple
In this tutorial, we learned How to get Last N Characters in String in Kotlin language with well detailed examples.