contains
operator fun CharSequence.contains(other: CharSequence, ignoreCase: Boolean = false): Boolean(source)
Returns true
if this char sequence contains the specified other sequence of characters as a substring.
Since Kotlin
1.0Parameters
ignoreCase
true
to ignore character case when comparing strings. By default false
.
Samples
import java.util.Locale
import java.util.regex.Pattern
import kotlin.test.*
fun main() {
//sampleStart
val string = "Kotlin 2.2.0"
println("\"K\" in string is ${"K" in string}") // true
// The string only contains capital K
println("\"k\" in string is ${"k" in string}") // false
// However, it will be located if the case is ignored
println("string.contains(\"k\", ignoreCase = true) is ${string.contains("k", ignoreCase = true)}") // true
// Every string contains an empty string
println("\"\" in string is ${"" in string}") // true
// The string contains itself ...
println("string in string is ${string in string}") // true
// ... even if it is empty
println("\"\" in \"\" is ${"" in ""}") // true
// String's prefix is shorter than a string, so it can't contain it
println("string in \"Kotlin\" is ${string in "Kotlin"}") // false
//sampleEnd
}
Returns true
if this char sequence contains the specified character char.
Since Kotlin
1.0Parameters
ignoreCase
true
to ignore character case when comparing characters. By default false
.
Returns true
if this char sequence contains at least one match of the specified regular expression regex.