Kotlin String findLast()

Kotlin String findLast() Tutorial

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

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

Syntax

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

inline fun CharSequence.findLast(
    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.findLast() 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 last character that satisfies the condition is then returned by the findLast() function.

Examples for String findLast() function

1. Find the Last Uppercase Letter in the given String

In this example, we’ll use findLast() to find the last 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 findLast() function on text with the predicate function as an argument. The function returns the last 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 Banana"

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

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

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

Output

Original String:
Hello World Banana

Last Uppercase Letter: B

2. Find the Last Digit in the given String

In this example, we’ll use findLast() to find the last 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 findLast() function on text with the predicate function as an argument. The function returns the last 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 = "12345 hello world"

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

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

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

Output

Original String:
12345 hello world

Last Digit: 5

Summary

In this tutorial, we’ve covered the findLast() function in Kotlin strings, its syntax, and how to use it to find the last 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.