Working with Ranges in Kotlin: Integers, Characters, and Custom Steps
A Range in Kotlin is a closed interval between two ednpoints. It is created with the .. operator and supports the membership checks in and !in. Any value that is greater than or equal to the lower bound and less than or equal to the upper bound is considered part of the range.
val lowercase = 'a'..'z'
val digits = 1..9
The expression 'a' in lowercase returns true, while 'A' in lowercase returns false. Likewise, 9 in digits is true but 10 in digits is false.
Iterating over numeric ranges
Integral ranges such as IntRange, LongRange, and CharRange are optimized by the compiler in to efficient :for loops.
fun main() {
for (i in 1..5) print(i) // 12345
println()
for (ch in 'a'..'f') print(ch) // abcdef
println()
val realSpan = 1.0..5.0
println(realSpan) // 1.0..5.0
println("3.14 contained? ${3.14 in realSpan}") // true
}
Descending ranges
Using .. with a larger start value and smaller end value yields an empty range. To count downward, use downTo.
for (i in 5 downTo 1) print(i) // 54321
Excluding the upper bound
The until function creates a half-open interval that omits the final element.
for (i in 1 until 5) print(i) // 1234
Multiple ways to express the same range
fun main() {
// ascending
for (i in 1..5) print(i)
println()
for (i in 1.rangeTo(5)) print(i)
println()
// descending
for (i in 5 downTo 1) print(i)
println()
for (i in 5.downTo(1)) print(i)
println()
}
Character ranges
fun main() {
for (ch in 'a'..'e') print("$ch ") // a b c d e
println()
for (ch in 'e' downTo 'a') print("$ch ") // e d c b a
}
Custom step size
The step modifier skips values at regular intervals.
fun main() {
for (i in 1..10 step 2) print("$i ") // 1 3 5 7 9
println()
for (i in 10 downTo 1 step 3) print("$i ") // 10 7 4 1
}
Using an explicit iterator
Ranges implement Iterable, so you can obtain an Iterator and traverse it manually.
fun main() {
val letters = 'a'..'e'
val iter = letters.iterator()
while (iter.hasNext()) {
print("${iter.next()} ")
}
}
// Output: a b c d e