🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
filterFunction Syntax- Understanding
filter - Examples
- Basic Usage
- Filtering Vowels
- Filtering Digits
- Real-World Use Case
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment