elementAtOrElse
Returns a character at the given index or the result of calling the defaultValue function if the index is out of bounds of this char sequence.
Since Kotlin
1.0Samples
import kotlin.test.*
fun main() {
//sampleStart
val phone = "+263783"
// in-bounds indices return the character at that position
println(phone.elementAtOrElse(0) { 'X' }) // +
println(phone.elementAtOrElse(phone.lastIndex) { 'X' }) // 3
// an out-of-bounds index evaluates the fallback lambda
println(phone.elementAtOrElse(-1) { if (it < 0) '<' else '>' }) // <
println(phone.elementAtOrElse(8) { if (it < 0) '<' else '>' }) // >
// empty string always evaluates the fallback lambda
println("".elementAtOrElse(0) { 'X' }) // X
//sampleEnd
}Content copied to clipboard