Joining strings in Kotlin

Level 3115 keystrokes

joinToString() turns the elements of a list into one string. Without arguments it uses ", " as the separator; you can pass your own separator such as " | ".

Kotlin code

// A list of three strings
1val fruits = listOf("apple", "banana", "cherry")
// The default separator is ", ": prints apple, banana, cherry
2println(fruits.joinToString())
// Pass a custom separator: prints apple | banana | cherry
3println(fruits.joinToString(" | "))

Output

apple, banana, cherry
apple | banana | cherry
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • "× 8
  • (× 5
  • )× 5
  • ,× 2
  • .× 2
  • =× 1
  • |× 1

More Kotlin examples at level 3

← Kotlin typing practice