Library contents

Kotlin List.map

Trim every badge name while preserving registration order.

A simple example

List.map

  1. Start with " A ", " B "
  2. Do this Trim each element
  3. Result "A", "B"
Applying the same text operation to each element preserves the list order.

Syntax

Kotlin syntax
names.map { it.trim() }
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

Clean the names on two visitor badges

A reception desk receives visitor names copied from an email. Extra spaces at the edges would make the printed badges look uneven, so the desk cleans each name while preserving arrival order.

From the programval names = listOf(" Mira ", " Sam")
ResultMira, Sam
Lesson.kt
fun main() {
    val names = listOf(" Mira ", " Sam")
    println(names.map { it.trim() }.joinToString(", "))
}
Output
Mira, Sam

The cleaned names are ready for the badge printer.

Advertisements

Clean a volunteer roster

Two names pasted from a form have stray spaces. The printer trims surrounding spaces from each label. Which names remain on the badge sheet?

Given“ Isa” · “Lou ”
Worked resultIsa · Lou

Prepare two exhibit labels

A curator copies padded names out of a spreadsheet. The printer trims surrounding spaces from each label. Which names remain on the badge sheet?

Given“ Clay ” · “ Ink”
Worked resultClay · Ink

What to watch for

Store or consume the returned list. The input names are unchanged.

Documentation for List.map