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.0

Parameters

ignoreCase

true to ignore character case when comparing strings. By default false.

Samples

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
}

operator fun CharSequence.contains(char: Char, ignoreCase: Boolean = false): Boolean(source)

Returns true if this char sequence contains the specified character char.

Since Kotlin

1.0

Parameters

ignoreCase

true to ignore character case when comparing characters. By default false.

Samples

import kotlin.test.*

fun main() { 
   //sampleStart 
   val text = "kotlin"

// The string contains lowercase 'k'
println("'k' in text is ${'k' in text}") // true

// The string does not contain uppercase 'K' (case-sensitive check)
println("'K' in text is ${'K' in text}") // false

// However, it will be located if the case is ignored
println("text.contains('K', ignoreCase = true) is ${text.contains('K', ignoreCase = true)}") // true

// The string does not contain 'z'
println("'z' in text is ${'z' in text}") // false 
   //sampleEnd
}

inline operator fun CharSequence.contains(regex: Regex): Boolean(source)

Returns true if this char sequence contains at least one match of the specified regular expression regex.

Since Kotlin

1.0