To remove the last N characters of a string in Kotlin, you can use the substring method with the range from 0 to length - N.
In this example,
str1
with a value of "Hello World"
.fun main(args: Array<String>) {
val str1 = "Hello World"
val modifiedStr = str1.substring(0, str1.length - 3)
println("Modified string is: $modifiedStr")
}
Modified string is: Hello Wo
In this example,
str2
with a value of "apple"
.fun main(args: Array<String>) {
val str2 = "apple"
val modifiedStr = str2.substring(0, str2.length - 2)
println("Modified string is: $modifiedStr")
}
Modified string is: app
In this tutorial, we learned How to Remove last N Characters in a String in Kotlin language with well detailed examples.