elementAt

expect fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.1

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.0

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.3

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual inline fun CharSequence.elementAt(index: Int): Char(source)

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

Since Kotlin

1.8

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = "kotlin"
println(string.elementAt(0)) // k
println(string.elementAt(5)) // n
// string.elementAt(-1) // will fail with IndexOutOfBoundsException
// string.elementAt(6) // will fail with IndexOutOfBoundsException

val empty = ""
// empty.elementAt(0) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}