Kotlin String filter

The filter function in Kotlin is used to create a new string containing only those characters from the original string that match a given predicate. This function belongs to the String class in the Kotlin standard library and provides a way to filter characters based on a specified condition.

Table of Contents

  1. Introduction
  2. filter Function Syntax
  3. Understanding filter
  4. Examples
    • Basic Usage
    • Filtering Vowels
    • Filtering Digits
  5. Real-World Use Case
  6. Conclusion

Introduction

The filter function allows you to create a new string by including only the characters that satisfy a specified condition. This is useful for tasks such as removing unwanted characters, extracting specific types of characters, and cleaning up strings.

filter Function Syntax

The syntax for the filter function is as follows:

inline fun CharSequence.filter(predicate: (Char) -> Boolean): String

Parameters:

  • predicate: A lambda function that takes a character and returns a Boolean indicating whether the character should be included in the resulting string.

Returns:

  • A new string containing only the characters that match the predicate.

Understanding filter

The filter function iterates over each character in the string, applies the predicate function to it, and includes it in the result if the predicate returns true. The result is a new string containing only the characters that satisfy the condition specified by the predicate.

Examples

Basic Usage

To demonstrate the basic usage of filter, we will create a new string containing only the lowercase letters from the original string.

Example

fun main() {
    val text = "Hello, World!"
    val filteredText = text.filter { it.isLowerCase() }
    println("Original text: $text")
    println("Filtered text: $filteredText")
}

Output:

Original text: Hello, World!
Filtered text: elloorld

Filtering Vowels

This example shows how to filter out only the vowels from a string.

Example

fun main() {
    val text = "Kotlin Programming"
    val vowels = "AEIOUaeiou"
    val filteredText = text.filter { it in vowels }
    println("Original text: $text")
    println("Filtered text (vowels): $filteredText")
}

Output:

Original text: Kotlin Programming
Filtered text (vowels): oiaoa

Filtering Digits

This example demonstrates how to filter out only the digits from a string.

Example

fun main() {
    val text = "User1234"
    val filteredText = text.filter { it.isDigit() }
    println("Original text: $text")
    println("Filtered text (digits): $filteredText")
}

Output:

Original text: User1234
Filtered text (digits): 1234

Real-World Use Case

Cleaning Up User Input

In real-world applications, the filter function can be used to clean up user input by removing unwanted characters, such as whitespace or special characters.

Example

fun main() {
    val userInput = "  user@#name!!  "
    val cleanedInput = userInput.filter { it.isLetterOrDigit() }
    println("Original input: '$userInput'")
    println("Cleaned input: '$cleanedInput'")
}

Output:

Original input: '  user@#name!!  '
Cleaned input: 'username'

Conclusion

The filter function in Kotlin's String class is a powerful method for creating new strings by including only the characters that match a specified condition. It provides a simple way to remove unwanted characters, extract specific types of characters, and clean up strings for various applications. 

By understanding and using this function, you can effectively manage character filtering operations in your Kotlin applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare