elementAtOrNull

Returns a character at the given index or null if the index is out of bounds of this char sequence.

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val lion = "Lion"

// safe access returns char at the given index
println(lion.elementAtOrNull(0)) // L
println(lion.elementAtOrNull(lion.lastIndex)) // n

// index out of bounds returns null
println(lion.elementAtOrNull(-1)) // null
println(lion.elementAtOrNull(20)) // null

// empty string always returns null
println("".elementAtOrNull(0)) // null 
   //sampleEnd
}