Kotlin String - contains() Function



The Kotlin string contains() function is used to check whether a string or character sequence contains the specified other sequence of characters as a substring or specified character. The function supports case-sensitive and case-insensitive comparisons, so it can be used to verify characters or analyze strings.

This function also checks the regular expression regex and returns true if there is at least one match of the specified regex in this char sequence.

Syntax

Following is the syntax of the Kotlin string contains() function −

fun CharSequence.contains(other: CharSequence, ignoreCase: Boolean = false): Boolean
fun CharSequence.contains(char: Char, ignoreCase: Boolean = false): Boolean
fun CharSequence.contains(regex: Regex): Boolean

Parameters

This function accepts following parameters −

  • other: The substring or character sequence to search for.
  • char: A single character to check within the string.
  • ignoreCase (optional): A Boolean flag to check whether the search should be case-insensitive. Default is false.

Return value

This function returns boolean value, true if the string contains the specifies substring or character; otherwise, false.

Example 1: Check Substring Exists within a String

Following is the basic example, here we use contains() to check whether specified substring contains in current string in kotlin −

fun main() {
   val string = "Hello, Kotlin"
   val substring = "Kot"
   val flag = string.contains(substring)
   println("Is this substring available: ${flag}")
}

Output

Following is the output −

Is this substring available: true

Example 2: Check Char Exists within a String

In this example, check whether the specified character is available in this string using the contains() function in Kotlin −

fun main() {
   val string = "Hello tutorialspoint!"
   val flag = string.contains('s')
   println("Is this character available: ${flag}")
}

Output

Following is the output −

Is this character available: true

Example 3: If the Case is Sensitive or Insensitive

In this example, the function returns true if it is case sensitive. If the case is sensitive, it returns false unless ignoreCase is mentioned. If ignoreCase is true, then it returns true −

fun main() {
   val string = "tutorialspoint"
   // when case-sensitive
   val flag1 = string.contains('p')
   println("when case-sensitive: $flag1")
   // when case-insensitive
   val flag2 = string.contains('P', ignoreCase = true)
   println("when case-insensitive: $flag2")
}

Output

Following is the output −

when case-sensitive: true
when case-insensitive: true

Example 4: Use Contains() with Regex

In this example, we use the contains() to check regx pattern −

fun main() {
   val string = "Welcome to tutorialspoint, learn Kotlin!"
   
   // Define a regex pattern to check for the word "tutorials" followed by any character(s)
   val regex = Regex("tutorials.*")

   // Check if the string contains the pattern
   val result = regex.containsMatchIn(string)

   println("Does the string contain the pattern? $result")
}

Output

Following is the output −

Does the string contain the pattern? true
kotlin_strings.htm
Advertisements