Skip to main content

Access Characters of a String in Kotlin

In order to access the characters of a string in Kotlin, there are a few different techniques that you can use. The first approach is to ndex] to access the characters at the position. Alternatively, you can loop through each individual character using the forEach() method.

val str = "Svalbard & Jan Mayen Islands";
println(str[5]) //"a"
println(str[9]) //"&"

for (char in str) { println(char ) }

val myString = "Hello, Kotlin"

for (char in myString) {
    // Access each character in the string
    println(char)
}
val myString = "Hello, Kotlin"

// Access the first character
val firstChar = myString[0] // 'H'

// Access the fifth character
val fifthChar = myString[4] // 'o'

By continuing to use the site, you agree to the use of cookies.