Kotlin String onEachIndexed()

Kotlin String onEachIndexed() Tutorial

The String.onEachIndexed() function in Kotlin is used to perform the specified action for each character in the string along with its index. It applies the provided action to each character and its corresponding index sequentially and returns the original string.

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

Syntax

The syntax of the String onEachIndexed() function is as follows:

inline fun <S : CharSequence> S.onEachIndexed(
    action: (index: Int, Char) -> Unit
): S

where

ParameterDescription
actionA function that defines the action to be performed on each character and its index. It takes the index and character as arguments and returns Unit.
Parameter of String.onEachIndexed() function

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

Examples for String onEachIndexed() function

1. Print Each Character with Index using onEachIndexed()

In this example, we’ll use onEachIndexed() to print each character of a given string along with its index.

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

Kotlin Program

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

    // Defining an action to print each character with index
    val action: (index: Int, Char) -> Unit = { index, char -> println("[$index] $char") }

    // Using onEachIndexed() to print each character and index
    text.onEachIndexed(action)
}

Output

[0] H
[1] e
[2] l
[3] l
[4] o
[5]  
[6] W
[7] o
[8] r
[9] l
[10] d

2. Count Uppercase Letters with Index in give String

In this example, we’ll use onEachIndexed() to count the number of uppercase letters in a given string and print their indices.

  1. Take a string value in text.
  2. Define an action function that increments a counter for uppercase letters and prints their index.
  3. Call onEachIndexed() function on text with the action function as an argument. The function counts the uppercase letters, prints their index, and returns the original string.
  4. You may print the counter and the original string to the console output.

Kotlin Program

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

    // Counter for uppercase letters
    var uppercaseCount = 0

    // Defining an action to count uppercase letters and print their index
    val action: (index: Int, Char) -> Unit = { index, char ->
        if (char.isUpperCase()) {
            uppercaseCount++
            println("Uppercase Letter at index [$index]: $char")
        }
    }

    // Using onEachIndexed() to count uppercase letters, print their index, and return the original string
    text.onEachIndexed(action)

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

Output

Uppercase Letter at index [0]: H
Uppercase Letter at index [6]: W
Original String:
Hello World
Uppercase Letters Count: 2

Summary

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