Kotlin String map

The map function in Kotlin is used to transform each character in a string into another value based on a given transformation function. This function belongs to the String class in the Kotlin standard library and provides a way to apply a function to each character in the string, producing a list of transformed values.

Table of Contents

  1. Introduction
  2. map Function Syntax
  3. Understanding map
  4. Examples
    • Basic Usage
    • Converting Characters to Their Unicode Values
    • Mapping Characters to Their Uppercase Equivalents
  5. Real-World Use Case
  6. Conclusion

Introduction

The map function transforms each character in a string into another value based on a specified function. This is useful for tasks such as character manipulation, encoding, and transforming characters to other representations.

map Function Syntax

The syntax for the map function is as follows:

inline fun <R> CharSequence.map(transform: (Char) -> R): List<R>

Parameters:

  • transform: A lambda function that takes a character and returns a transformed value of type R.

Returns:

  • A list of transformed values.

Understanding map

The map function iterates over each character in the string, applies the transform function to each character, and collects the results into a list. The type of the resulting list depends on the return type of the transform function.

Examples

Basic Usage

To demonstrate the basic usage of map, we will transform each character in a string to its corresponding ASCII value.

Example

fun main() {
    val text = "Kotlin"
    val asciiValues = text.map { it.toInt() }
    println("Original text: $text")
    println("ASCII values: $asciiValues")
}

Output:

Original text: Kotlin
ASCII values: [75, 111, 116, 108, 105, 110]

Converting Characters to Their Unicode Values

This example shows how to convert each character in a string to its Unicode value.

Example

fun main() {
    val text = "Hello"
    val unicodeValues = text.map { it.code }
    println("Original text: $text")
    println("Unicode values: $unicodeValues")
}

Output:

Original text: Hello
Unicode values: [72, 101, 108, 108, 111]

Mapping Characters to Their Uppercase Equivalents

This example demonstrates how to transform each character in a string to its uppercase equivalent using map.

Example

fun main() {
    val text = "Kotlin"
    val uppercaseChars = text.map { it.uppercaseChar() }
    println("Original text: $text")
    println("Uppercase characters: $uppercaseChars")
}

Output:

Original text: Kotlin
Uppercase characters: [K, O, T, L, I, N]

Real-World Use Case

Encoding a String

In real-world applications, the map function can be used to encode a string by transforming each character based on a specific encoding scheme.

Example

fun main() {
    val text = "Encode"
    val encodedText = text.map { it + 1 }.joinToString("")
    println("Original text: $text")
    println("Encoded text: $encodedText")
}

Output:

Original text: Encode
Encoded text: Fodpef

Conclusion

The map function in Kotlin's String class is a versatile method for transforming each character in a string based on a specified function. It provides a simple way to perform character manipulation, encoding, and other transformations for various applications. 

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

Comments