Kotlin – Convert comma-separated String to List & List to String

This Kotlin tutorial shows you way to convert comma-separated String into List & vice versa: convert List into one String with jointostring() example.

Related Posts:
Kotlin List & Mutable List tutorial with examples
Kotlin – Sort List of custom Objects

Convert comma-separated String to List

We use CharSequence.split() function that returns a List of String, then map() each item with CharSequence.trim() function to remove [space] character.

val input: String = "One, Two, Three, Four, Five"

println("=== String into List ===")
var result: List = input.split(",").map { it.trim() }

result.forEach { println(it) }

This is the result-

=== String into List ===
One
Two
Three
Four
Five

joinToString() function

The jointostring() function helps us to convert an Kotlin List or Array to one string.

We can specify the separator, prefix, postfix.

If the List could have a large number of string items, we can provide a non-negative limit value, followed by the truncated string.

This is the protoype of joinToString() function:

fun joinToString(
    separator: CharSequence = ", ",
    prefix: CharSequence = "",
    postfix: CharSequence = "",
    limit: Int = -1,
    truncated: CharSequence = "...",
    transform: ((Char) -> CharSequence)? = null
): String

Let’s look at some examples of how to use joinToString() function:

val chars = listOf('a', 'b', 'c', 'd', 'e')
// [a, b, c, d, e]

chars.joinToString()
// a, b, c, d, e

chars.joinToString(prefix = "{", postfix = "}")
// {a, b, c, d, e}

chars.joinToString(prefix = "{", postfix = "}", separator = "-")
// {a-b-c-d-e}

chars.joinToString(prefix = "{", postfix = "}", separator = "-") { it -> it.toUpperCase().toString() }
// {A-B-C-D-E}

val nums = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)

nums.joinToString(limit = 5, truncated = "<...>")
// 1, 2, 3, 4, 5, <...>
	
nums.joinToString(limit = 5, truncated = "<...>") { it -> (it * 2).toString() }
// 2, 4, 6, 8, 10, <...>

joinToString() transform example

Now I will show you two ways to use joinToString() transform:

  • pass it as a transform parameter into function
  • use it outside the function
val chars = listOf('a', 'b', 'c', 'd', 'e')
// [a, b, c, d, e]

chars.joinToString()
// a, b, c, d, e

chars.joinToString(separator = "-", transform = { it -> it.toUpperCase().toString() })
// A-B-C-D-E

chars.joinToString(separator = "|", transform = { it.toUpperCase().toString() })
// A|B|C|D|E

chars.joinToString(separator = "-") { it -> it.toUpperCase().toString() }
// A-B-C-D-E

chars.joinToString(separator = "|") { it.toUpperCase().toString() }
// A|B|C|D|E

Convert List to String

Now, we’re gonna use joinToString() function above with prefix, postfix, limit 3 elements to be appended, and truncated by string “…more…”.

val myList = listOf("One", "Two", "Three", "Four, Five")
println(myList)

println("=== List into String ===")
val output = myList.joinToString("|", prefix = "<", postfix = ">", limit = 3, truncated = "...more...")

println(output)

The result:

[One, Two, Three, Four, Five]
=== List into String ===

Conclusion

Let’s me summarize the tutorial in some lines.

– To convert comma-separated String to List:

  • use CharSequence.split() to split string into List
  • then call map() and pass transform function like trim or something to refine each item of the List

– To convert List to String:

  • use joinToString() function with separator, prefix, postfix
  • if the number of items in the List is large, you can pass value for limit & truncated
  • use additional transform function as parameter to change each item of the List

Happy Learning! See you again.

Further Reading