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
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 excluded and false for characters to be included. |
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.
- Take a string value in
originalString
. - Define a predicate function that returns
true
for vowels andfalse
for non-vowel characters. - Call
filterNot()
function onoriginalString
with the predicate function as the argument. The function returns a new string with non-vowel characters. - 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.
- Take a string value in
text
. - Define a predicate function that returns
true
for digits andfalse
for non-digit characters. - Call
filterNot()
function ontext
with the predicate function as the argument. The function returns a new string with non-digit characters. - 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.