To remove a character at a specific index in a string in Kotlin, you can use string slicing.
In this example,
str1
with a value of "Hello World"
.removeRange
method of the String
class to remove the character at the specified index.fun main() {
var str1 = "Hello World"
val indexToRemove = 6
str1 = str1.removeRange(indexToRemove, indexToRemove + 1)
println("Modified string is: $str1")
}
Modified string is: Hello orld
In this example,
str2
with a value of "apple"
.removeRange
method of the String
class to remove the character at the specified index.fun main() {
var str2 = "apple"
val indexToRemove = 2
str2 = str2.removeRange(indexToRemove, indexToRemove + 1)
println("Modified string is: $str2")
}
Modified string is: aple
In this tutorial, we learned How to Remove Character at specific Index in a String in Kotlin language with well detailed examples.