Kotlin String contains

The contains function in Kotlin is used to check if a string contains a specified substring or character. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to perform substring search operations within a string.

Table of Contents

  1. Introduction
  2. contains Function Syntax
  3. Understanding contains
  4. Examples
    • Basic Usage
    • Using contains with Ignore Case
    • Checking for Characters
  5. Real-World Use Case
  6. Conclusion

Introduction

The contains function checks if a string contains a specified substring or character. This is useful for various string operations such as validation, search, and filtering.

contains Function Syntax

The syntax for the contains function is as follows:

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

Parameters:

  • other: The substring to search for.
  • char: The character to search for.
  • ignoreCase: If true, the search will ignore case differences (default is false).

Returns:

  • true if the string contains the specified substring or character, false otherwise.

Understanding contains

The contains function searches for the specified substring or character within the string. If the substring or character is found, it returns true; otherwise, it returns false. The ignoreCase parameter allows for case-insensitive searches.

Examples

Basic Usage

To demonstrate the basic usage of contains, we will create a string and check if it contains a specific substring.

Example

fun main() {
    val text = "Hello, World!"
    val containsHello = text.contains("Hello")
    val containsKotlin = text.contains("Kotlin")

    println("Does the text contain 'Hello'? $containsHello")
    println("Does the text contain 'Kotlin'? $containsKotlin")
}

Output:

Does the text contain 'Hello'? true
Does the text contain 'Kotlin'? false

Using contains with Ignore Case

This example shows how to use contains with the ignoreCase parameter for case-insensitive searches.

Example

fun main() {
    val text = "Hello, World!"
    val containsHelloCaseInsensitive = text.contains("hello", ignoreCase = true)
    val containsWorldCaseSensitive = text.contains("world", ignoreCase = false)

    println("Does the text contain 'hello' (ignore case)? $containsHelloCaseInsensitive")
    println("Does the text contain 'world' (case sensitive)? $containsWorldCaseSensitive")
}

Output:

Does the text contain 'hello' (ignore case)? true
Does the text contain 'world' (case sensitive)? false

Checking for Characters

This example demonstrates how to use contains to check for the presence of a specific character.

Example

fun main() {
    val text = "Kotlin Programming"
    val containsK = text.contains('K')
    val containsZ = text.contains('Z')

    println("Does the text contain 'K'? $containsK")
    println("Does the text contain 'Z'? $containsZ")
}

Output:

Does the text contain 'K'? true
Does the text contain 'Z'? false

Real-World Use Case

Validating User Input

In real-world applications, the contains function can be used to validate user input, such as checking if an email address contains the '@' symbol.

Example

fun main() {
    val email = "user@example.com"

    if (email.contains('@')) {
        println("Valid email address.")
    } else {
        println("Invalid email address.")
    }
}

Output:

Valid email address.

Conclusion

The contains function in Kotlin's String class is a convenient method for checking if a string contains a specified substring or character. It provides a simple way to perform search operations within strings and can be used for various applications such as validation and filtering. By understanding and using this function, you can effectively manage substring searches in your Kotlin applications.

Comments