Kotlin String filterTo() Tutorial
The String.filterTo()
function in Kotlin is used to filter characters from a string based on a given predicate and add the results to a destination variable. It filters the characters based on the specified condition and appends the results to the provided destination variable.
In this tutorial, we’ll explore the syntax of the filterTo()
function and provide examples of its usage in Kotlin strings.
Syntax
The syntax of the filterTo()
function is as follows:
fun String.filterTo(
destination: C,
predicate: (Char) -> Boolean
): C
where
Parameter | Description |
---|---|
destination | The destination to which the filtered characters are added. |
predicate | A function that defines the condition for filtering characters. It takes a character as an argument 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 value. The function returns true
for characters that meet the specified condition and false
for those that do not. The filtered characters are then added to the specified destination collection.
Examples for String filterTo() function
1. Filter Vowels from String to a StringBuilder
In this example, we’ll use filterTo()
to filter out vowels from a string and add the vowel characters to a destination StringBuilder.
- Take a string value in
originalString
. - Create an empty StringBuilder as the destination collection, e.g.,
vowelString
. - Define a predicate function that returns
true
for vowels andfalse
for non-vowel characters. - Call
filterTo()
function onoriginalString
with the destination and predicate function as arguments. The function adds vowel characters to the destination. - You may print the resulting destination to the console output.
Kotlin Program
fun main() {
val originalString = "Hello World"
// Create an empty StringBuilder as the destination
val vowelString = StringBuilder()
// Defining a predicate to filter vowels
val predicate: (Char) -> Boolean = { it in "aeiouAEIOU" }
// Using filterTo() to filter only vowels from the original string and add to the destination
originalString.filterTo(vowelString, predicate)
// Printing the original string and the resulting destination
println("Original String:\n$originalString\n")
println("Vowel String :\n$vowelString")
}
Output
Original String:
Hello World
Vowel String :
eoo
2. Filter Digits in String to a Destination Variables
In this example, we’ll use filterTo()
to filter out digits from a string and add the digit characters to a destination variable.
- Take a string value in
text
. - Create an empty StringBuilder as the destination collection, e.g.,
digitCharacters
. - Define a predicate function that returns
true
for digits andfalse
for non-digit characters. - Call
filterTo()
function ontext
with the destination and predicate function as arguments. The function adds digit characters to the destination. - You may print the resulting destination to the console output.
Kotlin Program
fun main() {
val originalString = "Hello 123 World 456"
// Create an empty StringBuilder as the destination
val digitCharacters = StringBuilder()
// Defining a predicate to filter digits
val predicate: (Char) -> Boolean = { it.isDigit() }
// Using filterTo() to filter only digits from the original string and add to the destination
originalString.filterTo(digitCharacters, predicate)
// Printing the original string and the resulting destination
println("Original String:\n$originalString\n")
println("Digits :\n$digitCharacters")
}
Output
Original String:
Hello 123 World 456
Digits :
123456
Summary
In this tutorial, we’ve covered the filterTo()
function in Kotlin strings, its syntax, and how to use it to filter characters based on a specified condition and add it to a destination variable