Kotlin String find()

Kotlin String find() Tutorial

The String.find() function in Kotlin is used to find the first character in the string that matches a given predicate. It returns the first character that satisfies the specified condition, or null if no such character is found.

In this tutorial, we’ll explore the syntax of the find() function and provide examples of its usage in Kotlin strings.

Syntax

The syntax of the find() function is as follows:

fun CharSequence.find(
    predicate: (Char) -> Boolean
): Char?

where

ParameterDescription
predicateA function that defines the condition for selecting a character. It takes a character as an argument and returns true if the character satisfies the condition, and false otherwise.
Parameter of String.find() function

The predicate parameter is a function that determines whether a character should be selected. The function returns true for characters that meet the specified condition and false for those that do not. The first character that satisfies the condition is then returned by the find() function.

Examples for String find() function

1. Find the First Uppercase Letter in given String

In this example, we’ll use find() to find the first uppercase letter in a given string.

  1. Take a string value in text.
  2. Define a predicate function that returns true for uppercase letters and false for other characters.
  3. Call find() function on text with the predicate function as an argument. The function returns the first uppercase letter in the string, or null if none is found.
  4. You may print the resulting character to the console output.

Kotlin Program

fun main() {
    val text = "hello World"

    // Defining a predicate to select uppercase letters
    val predicate: (Char) -> Boolean = { it.isUpperCase() }

    // Using find() to find the first uppercase letter in the string
    val result = text.find(predicate)

    // Printing the original string and the resulting character
    println("Original String:\n$text\n")
    println("First Uppercase Letter: $result")
}

Output

Original String:
hello World

First Uppercase Letter: W

2. Find the First Digit in given String

In this example, we’ll use find() to find the first digit in a given string.

  1. Take a string value in text.
  2. Define a predicate function that returns true for digits and false for other characters.
  3. Call find() function on text with the predicate function as an argument. The function returns the first digit in the string, or null if none is found.
  4. You may print the resulting character to the console output.

Kotlin Program

fun main() {
    val text = "hello world 54321"

    // Defining a predicate to select digits
    val predicate: (Char) -> Boolean = { it.isDigit() }

    // Using find() to find the first digit in the string
    val result = text.find(predicate)

    // Printing the original string and the resulting character
    println("Original String:\n$text\n")
    println("First Digit: $result")
}

Output

Original String:
hello world 54321

First Digit: 5

Summary

In this tutorial, we’ve covered the find() function in Kotlin strings, its syntax, and how to use it to find the first character that satisfies a specified condition in a string. This function is useful for extracting specific elements from a string based on a given predicate.