Kotlin String onEach()

Kotlin String onEach() Tutorial

The String.onEach() function in Kotlin is used to perform the specified action for each character in the string. It applies the provided action to each character sequentially and returns the original string.

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

Syntax

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

inline fun <S : CharSequence> S.onEach(
    action: (Char) -> Unit
): S

where

ParameterDescription
actionA function that defines the action to be performed on each character. It takes a character as an argument and returns Unit.
Parameter of String.onEach() function

The action parameter is a function that specifies the action to be performed on each character in the string. The function takes a character as an argument and returns Unit.

Examples for String onEach() function

1. Print Each Character in String using String.onEach()

In this example, we’ll use onEach() to print each character of a given string.

  1. Take a string value in text.
  2. Define an action function that prints each character to the console.
  3. Call onEach() function on text with the action function as an argument.
  4. You may observe the printed characters in the console output.

Kotlin Program

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

    // Defining an action to print each character
    val action: (Char) -> Unit = { char -> println(char) }

    // Using onEach() to print each character
    text.onEach(action)
}

Output

H
e
l
l
o
 
W
o
r
l
d

2. Count Uppercase and Lowercase Letters in String using String.onEach()

In this example, we’ll use onEach() to count the number of uppercase and lowercase letters in a given string.

  1. Take a string value in text.
  2. Define an action function that increments counters for uppercase and lowercase letters based on the character.
  3. Call onEach() function on text with the action function as an argument. The function counts the uppercase and lowercase letters and returns the original string.
  4. You may print the counters and the original string to the console output.

Kotlin Program

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

    // Counters for uppercase and lowercase letters
    var uppercaseCount = 0
    var lowercaseCount = 0

    // Defining an action to count uppercase and lowercase letters
    val action: (Char) -> Unit = { char ->
        when {
            char.isUpperCase() -> uppercaseCount++
            char.isLowerCase() -> lowercaseCount++
        }
    }

    // Using onEach() to count letters and return the original string
    text.onEach(action)

    // Printing the counters and the original string
    println("Original String:\n$text")
    println("Uppercase Letters Count: $uppercaseCount")
    println("Lowercase Letters Count: $lowercaseCount")
}

Output

Original String:
Hello World
Uppercase Letters Count: 2
Lowercase Letters Count: 8

Summary

In this tutorial, we’ve covered the onEach() function in Kotlin strings, its syntax, and how to use it to perform a specified action for each character in the string. This function is useful for applying custom logic to each character of a string without modifying the string itself.