Kotlin String.contains()

Kotlin String.contains() Tutorial

The String.contains() function in Kotlin is used to check whether a specific sequence of characters (substring) is present in a given string. It returns true if the substring is found, false otherwise.

This tutorial will explore the syntax of the String.contains() function and provide examples of its usage in Kotlin strings.

Syntax

The syntax of the String.contains() function is as follows:

fun String.contains(
    other: CharSequence,
    ignoreCase: Boolean = false
): Boolean

The contains() function takes two parameters:

ParameterDescription
otherThe substring to search for within the given string.
ignoreCaseOptional parameter. If set to true, the comparison is case-insensitive; if false (default), the comparison is case-sensitive.
Parameters of String.contains() function

Examples for String.contains() function

1. Checking if String contains specified Substring

In this example, we’ll take two strings. The first string is mainString, and the second string is otherString. We shall use String.contains() and check if the first string contains the second string as a substring.

Kotlin Program

fun main() {
    val mainString = "Hello, Kotlin!"
    val otherString = "Kotlin"

    // Using contains() to check for substring presence
    if (mainString.contains(otherString)) {
        println("The main string contains the other string.")
    } else {
        println("The main string does not contain the other string.")
    }
}

Output

The main string contains the other string.

Since otherString is present in mainString, the expression mainString.contains() returned true, and the if-block in if else statement is executed.

Now, let us take a string value in otherString such that it is not a substring of the mainString, and run the program.

Kotlin Program

fun main() {
    val mainString = "Hello, Kotlin!"
    val otherString = "World"

    // Using contains() to check for substring presence
    if (mainString.contains(otherString)) {
        println("The main string contains the other string.")
    } else {
        println("The main string does not contain the other string.")
    }
}

Output

The main string does not contain the other string.

Since otherString is not present in mainString, the expression mainString.contains() returned false, and the else-block in if else statement is executed.

2. Case-Insensitive Search

In this example, we shall make use of the ignoreCase parameter, and check if the string mainString contains the otherString as substring, where the search is done while ignoring the case of characters in the strings.

Kotlin Program

fun main() {
    val mainString = "HELLO, KOTLIN!"
    val otherString = "kotlin"

    // Using contains()
    if (mainString.contains(otherString, ignoreCase = true)) {
        println("The main string contains the other string.")
    } else {
        println("The main string does not contain the other string.")
    }
}

Output

The main string contains the other string.

When ignoring the case, "kotlin" and "KOTLIN" are same.

Summary

In this tutorial, we’ve covered the String.contains() function in Kotlin strings, its syntax, and how to use it to check for the presence of a substring within a given string. The optional ignoreCase parameter allows for case-sensitive or case-insensitive searches.