Kotlin String filter() Tutorial
The String.filter()
function in Kotlin is used to create a new string containing only the characters that satisfy a given predicate. It filters the characters based on the specified condition and returns a string consisting of the characters that meet the criteria.
In this tutorial, we’ll explore the syntax of the filter()
function and provide examples of its usage in Kotlin strings.
Syntax
The syntax of the filter()
function is as follows:
fun String.filter(
predicate: (Char) -> Boolean
): String
where
Parameter | Description |
---|---|
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 string. The function returns true
for characters that meet the specified condition and false
for those that do not.
Examples for String filter() function
1. Filter out Non-Letter Characters
In this example, we’ll use filter()
to create a new string by excluding non-letter characters from a given string.
- Take a string value in
originalString
. - Define a predicate function that returns
true
for letters andfalse
for non-letter characters. - Call
filter()
function onoriginalString
with the predicate function as the argument. The function returns a new string with only letter characters. - You may print the resulting string to the console output.
Kotlin Program
fun main() {
val originalString = "Hello123 World!"
// Defining a predicate to filter out non-letter characters
val predicate: (Char) -> Boolean = { it.isLetter() }
// Using filter() to include only letter characters
val resultString = originalString.filter(predicate)
// Printing the original string and the result
println("Original String:\n$originalString\n")
println("Result String:\n$resultString")
}
Output
Original String:
Hello123 World!
Result String:
HelloWorld
2. Filter out Vowels and keep only Consonants
In this example, we’ll use filter()
to create a new string by excluding vowels from a given string.
- Take a string value in
text
. - Define a predicate function that returns
false
for vowels andtrue
for non-vowel characters. - Call
filter()
function ontext
with the predicate function as the argument. The function returns a new string with only non-vowel characters. - You may print the resulting string to the console output.
Kotlin Program
fun main() {
val originalString = "Kotlin is awesome"
// Defining a predicate to filter out vowels
val predicate: (Char) -> Boolean = { it !in "aeiouAEIOU" }
// Using filter() to include only non-vowel characters
val resultString = originalString.filter(predicate)
// Printing the original string and the result
println("Original String:\n$originalString\n")
println("Result String:\n$resultString")
}
Output
Original String:
Kotlin is awesome
Result String:
Ktln s wsm
Summary
In this tutorial, we’ve covered the filter()
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.