To remove the last character of a string in Kotlin, you can use the substring method with the range from 0 to the length of the string minus 1.
In this example,
str1
with a value of "Hello World"
.fun main() {
var str1 = "Hello World"
str1 = str1.substring(0, str1.length - 1)
println("Modified string is: $str1")
}
Modified string is: Hello Worl
In this example,
str2
with a value of "apple"
.fun main() {
var str2 = "apple"
str2 = str2.substring(0, str2.length - 1)
println("Modified string is: $str2")
}
Modified string is: appl
In this tutorial, we learned How to Remove Last Character in a String in Kotlin language with well detailed examples.