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 )
}

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

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close