Kotlin String filterNot()

Kotlin String filterNot() Tutorial

The String.filterNot() function in Kotlin is used to create a new string containing only the characters that do not satisfy a given predicate. It filters out the characters based on the specified condition and returns a string consisting of the characters that do not meet the criteria.

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

Syntax

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

fun String.filterNot(
    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 excluded and false for characters to be included.
Parameter of String.filterNot() function

The predicate parameter is a function that determines whether a character should be excluded from the resulting string. The function returns true for characters that meet the specified condition and false for those that do not.

Examples for String filterNot() function

1. Filter Out Vowels

In this example, we’ll use filterNot() to create a new string by excluding vowels from a given string.

  1. Take a string value in originalString.
  2. Define a predicate function that returns true for vowels and false for non-vowel characters.
  3. Call filterNot() function on originalString with the predicate function as the argument. The function returns a new string with non-vowel characters.
  4. You may print the resulting string to the console output.

Kotlin Program

fun main() {
    val originalString = "Hello World"

    // Defining a predicate to filter out vowels
    val predicate: (Char) -> Boolean = { it in "aeiouAEIOU" }

    // Using filterNot() to exclude vowels from the original string
    val resultString = originalString.filterNot(predicate)

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

Output

Original String:
Hello World

Result String:
Hll Wrld

2. Filter Out Digits

In this example, we’ll use filterNot() to create a new string by excluding digits from a given string.

  1. Take a string value in text.
  2. Define a predicate function that returns true for digits and false for non-digit characters.
  3. Call filterNot() function on text with the predicate function as the argument. The function returns a new string with non-digit characters.
  4. You may print the resulting string to the console output.

Kotlin Program

fun main() {
    val originalString = "Hello 123 World 456"

    // Defining a predicate to filter out digits
    val predicate: (Char) -> Boolean = { it.isDigit() }

    // Using filterNot() to exclude digits from the original string
    val resultString = originalString.filterNot(predicate)

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

Output

Original String:
Hello 123 World 456

Result String:
Hello  World 

Summary

In this tutorial, we’ve covered the filterNot() function in Kotlin strings, its syntax, and how to use it to create a new string by excluding characters that meet a specified condition. This function provides a convenient way to filter out unwanted characters from a string based on custom criteria.