Kotlin String filterIndexed()

Kotlin String filterIndexed() Tutorial

The String.filterIndexed() function in Kotlin is used to create a new string containing only the characters that satisfy a given predicate along with their corresponding indices. It filters the characters based on the specified condition and returns a string consisting of the characters that meet the criteria, along with their respective indices.

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

Syntax

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

fun String.filterIndexed(
    predicate: (index: Int, char: Char) -> Boolean
): String

where

ParameterDescription
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.
Parameter of String.filterIndexed() function

The predicate parameter is a function that determines whether a character should be included in the resulting string based on both its value and index.

Examples for String filterIndexed() function

1. Filter out Even-Indexed Characters

In this example, we’ll use filterIndexed() to create a new string by including only characters with even indices from a given string.

  1. Take a string value in originalString.
  2. Define a predicate function that returns true for characters with even indices and false for characters with odd indices.
  3. Call filterIndexed() function on originalString with the predicate function as the argument. The function returns a new string with characters at even indices.
  4. You may print the resulting string to the console output.

Kotlin Program

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

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

    // Using filterIndexed() to include only characters with even indices
    val resultString = originalString.filterIndexed(predicate)

    // Printing the original string and the result
    println("Original String:\n$originalString\n")
    println("Result String:\n$resultString")
}

Output

Original String:
FilterIndexed Example

Result String:
FleIdxdEape

2. Filter Characters at Odd Indices

In this example, we’ll use filterIndexed() to create a new string by including only characters with odd indices from a given string.

  1. Take a string value in text.
  2. Define a predicate function that returns true for characters with odd indices and false for characters with even indices.
  3. Call filterIndexed() function on text with the predicate function as the argument. The function returns a new string with characters at odd indices.
  4. You may print the resulting string to the console output.

Kotlin Program

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

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

    // Using filterIndexed() to include only characters with odd indices
    val resultString = originalString.filterIndexed(predicate)

    // Printing the original string and the result
    println("Original String:\n$originalString\n")
    println("Result String:\n$resultString")
}

Output

Original String:
FilterIndexed Example

Result String:
itrnee xml

Summary

In this tutorial, we’ve covered the filterIndexed() function in Kotlin strings, its syntax, and how to use it to create a new string by including only the characters that satisfy a specified condition along with their respective indices. This function provides a flexible way to filter characters based on both value and position within the string.