Kotlin String filter()

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

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

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.

  1. Take a string value in originalString.
  2. Define a predicate function that returns true for letters and false for non-letter characters.
  3. Call filter() function on originalString with the predicate function as the argument. The function returns a new string with only letter characters.
  4. 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.

  1. Take a string value in text.
  2. Define a predicate function that returns false for vowels and true for non-vowel characters.
  3. Call filter() function on text with the predicate function as the argument. The function returns a new string with only non-vowel characters.
  4. 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.