Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 20 / 68

Splitting and Joining Strings (split, joinToString)

In this lesson you will learn to split and join strings in Kotlin so you can work with data such as CSV. This is for anyone searching for "Kotlin split joinToString how to use".

.split() breaks a string into a list. Going the other way, .joinToString() collects a list into a single string. Both come up constantly with CSV-style data.

The sample code splits a comma-separated string into a list with csv.split(","), then rebuilds it as a slash-separated string with fruits.joinToString(" / "). Combining the two lets you swap delimiters or strip out unwanted spaces.

A common stumbling block for beginners is the method name joinToString(). Unlike the join() of other languages, Kotlin names it this way and puts it on the list, which takes a moment to get used to.

In real-world development this pairing appears constantly in real business systems, from reading CSV files to parsing logs.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)