To get a character at a specific index in a string in Kotlin, you can use the get()
method or index the string directly.
In this example,
str
with a value of "Hello World"
.get()
method or by indexing the string directly.fun main() {
val str = "Hello World"
val charAtIndex3 = str.get(3)
println("Character at index 3: $charAtIndex3")
}
Character at index 3: l
In this example,
str
with a value of "Kotlin"
.get()
method or by indexing the string directly.fun main() {
val str = "Kotlin"
val charAtIndex1 = str[1]
println("Character at index 1: $charAtIndex1")
}
Character at index 1: o
In this tutorial, we learned How to get Character at Specific Index in String in Kotlin language with well detailed examples.