W3cubDocs

/Kotlin

elementAtOrNull

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun CharSequence.elementAtOrNull(index: Int): Char?

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

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf(1, 2, 3)
println(list.elementAtOrNull(0)) // 1
println(list.elementAtOrNull(2)) // 3
println(list.elementAtOrNull(3)) // null

val emptyList = emptyList<Int>()
println(emptyList.elementAtOrNull(0)) // null
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/element-at-or-null.html