contains

open operator fun contains(value: T): Boolean(source)

Checks whether the specified value belongs to the range.

A value belongs to the open-ended range if it is greater than or equal to the start bound and strictly less than the endExclusive bound.

Since Kotlin

1.9

Samples

import java.sql.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   val range = 1..<10
println("5 in range is ${5 in range}") // true
// The range includes the lower bound, but the upper bound is excluded
println("1 in range is ${1 in range}") // true
println("10 in range is ${10 in range}") // false
println("9 in range is ${9 in range}") // true
// Values lying outside bounds are not contained within the range
println("0 in range is ${0 in range}") // false
println("11 in range is ${11 in range}") // false

val stringRange = "AA"..<"ZZ"
println("\"AA\" in stringRange is ${"AA" in stringRange}") // true
println("\"AB\" in stringRange is ${"AB" in stringRange}") // true
println("\"ZZ\" in stringRange is ${"ZZ" in stringRange}") // false
// Note that "contains" relies on "compareTo" implementation, so in some cases results may not be intuitive.
// Here, "ZYX" is lexicographically greater than "AA", but lower than "ZZ", thus the range contains it.
println("\"ZYX\" in stringRange is ${"ZYX" in stringRange}") // true 
   //sampleEnd
}