Kotlin – Get character at specific index in String

Get character at specific index in String in Kotlin

To get the character at a specific index in a string in Kotlin, you can use String.get() function.

Call the gett() function on the given string str, and pass the index as argument, the character at which we are interested in the string.


The function returns the character in the string at specified index. But, if the string is not at least of that length, the get() function throws NoSuchElementException.


Get character at index=7 in the string

In the following program, we print two string values to output using print() function.

Kotlin Program

fun main(args: Array<String>) {
    val str = "Hello, World!"
    val index = 7
    val character = str.get(index)
    println("Character at index $index: $character")


Character at index 7: W