Kotlin String filterIndexedTo()

Kotlin String filterIndexedTo() Tutorial

The String.filterIndexedTo() function in Kotlin is used to filter characters from a string based on a given predicate along with their corresponding indices and add the results to a destination collection. It filters the characters based on the specified condition, includes their indices, and appends the results to the provided destination collection.

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

Syntax

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

fun String.filterIndexedTo(
    destination: C,
    predicate: (index: Int, char: Char) -> Boolean
): C

where

ParameterDescription
destinationThe destination collection to which the filtered characters are added.
predicateA function that defines the condition for filtering characters. It takes two parameters: the index of the character and the character itself, and returns true for characters to be included and false for characters to be excluded.
Parameters of String.filterIndexedTo() function

The predicate parameter is a function that determines whether a character should be included in the resulting collection based on both its value and index. The filtered characters are then added to the specified destination collection.

Examples for String filterIndexedTo() function

1. Filter and Add to a List

In this example, we’ll use filterIndexedTo() to filter characters from a string and add them to a destination list based on a given predicate.

  1. Take a string value in originalString.
  2. Create an empty StringBuilder as the destination, e.g., filteredString.
  3. Define a predicate function that returns true for characters with even indices and false for characters with odd indices.
  4. Call filterIndexedTo() function on originalString with the destination list and predicate function as arguments. The function adds characters at even indices to the destination StringBuilder.
  5. You may print the resulting StringBuilder to the console output.

Kotlin Program

fun main() {
    val originalString = "FilterIndexedTo Example"

    // Create an empty StringBuilder as the destination
    val filteredString = StringBuilder()

    // Defining a predicate to filter out characters with even indices
    val predicate: (index: Int, char: Char) -> Boolean = { index, _ -> index % 2 == 0 }

    // Using filterIndexedTo() to add characters with even indices to the destination
    originalString.filterIndexedTo(filteredString, predicate)

    // Printing the original string and the resulting destination list
    println("Original String:\n$originalString\n")
    println("Filtered String:\n${filteredString}")
}

Output

Original String:
FilterIndexedTo Example

Filtered String:
FleIdxdoEape

Summary

In this tutorial, we’ve covered the filterIndexedTo() function in Kotlin strings, its syntax, and how to use it to filter characters based on a specified condition and add the results to a destination collection. This function provides a convenient way to selectively collect characters along with their indices during the filtering process.