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
Parameter | Description |
---|---|
destination | The destination collection to which the filtered characters are added. |
predicate | A 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. |
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.
- Take a string value in
originalString
. - Create an empty StringBuilder as the destination, e.g.,
filteredString
. - Define a predicate function that returns
true
for characters with even indices andfalse
for characters with odd indices. - Call
filterIndexedTo()
function onoriginalString
with the destination list and predicate function as arguments. The function adds characters at even indices to the destination StringBuilder. - 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.