🎓 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 forEach function in Kotlin is used to perform an action on each character in a string. This function belongs to the String class in the Kotlin standard library and provides a way to iterate over each character in the string and apply a given operation.
Table of Contents
- Introduction
forEachFunction Syntax- Understanding
forEach - Examples
- Basic Usage
- Printing Characters with Indices
- Counting Specific Characters
- Real-World Use Case
- Conclusion
Introduction
The forEach function allows you to apply a given action to each character in a string. This is useful for tasks such as printing characters, performing calculations based on characters, and other character-based operations.
forEach Function Syntax
The syntax for the forEach function is as follows:
inline fun CharSequence.forEach(action: (Char) -> Unit)
Parameters:
action: A lambda function that takes a character and performs an operation on it.
Returns:
- This function does not return a value.
Understanding forEach
The forEach function iterates over each character in the string and applies the specified action function to it. This function is particularly useful for performing side effects such as printing or modifying external variables.
Examples
Basic Usage
To demonstrate the basic usage of forEach, we will print each character in a string.
Example
fun main() {
val text = "Kotlin"
text.forEach { char ->
println(char)
}
}
Output:
K
o
t
l
i
n
Printing Characters with Indices
This example shows how to print each character in a string along with its index.
Example
fun main() {
val text = "Kotlin"
text.forEachIndexed { index, char ->
println("Character at index $index: $char")
}
}
Output:
Character at index 0: K
Character at index 1: o
Character at index 2: t
Character at index 3: l
Character at index 4: i
Character at index 5: n
Counting Specific Characters
This example demonstrates how to count the number of occurrences of a specific character in a string using forEach.
Example
fun main() {
val text = "Kotlin is fun"
var count = 0
text.forEach { char ->
if (char == 'i') count++
}
println("Number of 'i' characters: $count")
}
Output:
Number of 'i' characters: 2
Real-World Use Case
Validating User Input
In real-world applications, the forEach function can be used to validate user input by checking each character against certain criteria.
Example
fun main() {
val userInput = "Username123"
var isValid = true
userInput.forEach { char ->
if (!char.isLetterOrDigit()) isValid = false
}
if (isValid) {
println("The input is valid.")
} else {
println("The input contains invalid characters.")
}
}
Output:
The input is valid.
Conclusion
The forEach function in Kotlin's String class is a useful method for performing actions on each character in a string. It provides a simple way to iterate over characters and apply operations such as printing, counting, and validation. By understanding and using this function, you can effectively manage character-based 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