Kaita John
Kaita John

Reputation: 1109

Print all elements in a comma separated String

I have a simple string that is generated dynamically as such:

String values = "one, two, three...last"

How to i print all items in string values?

Upvotes: 0

Views: 432

Answers (2)

deHaar
deHaar

Reputation: 18568

You could split and trim like this:

fun main() {
    val input: String = "one, two, three, four, five, last"
    // split the String by comma and trim each result
    var result: List<String> = input.split(",").map { it.trim() }
    // you can print the items of the list in a differently separated String again
    println(result.joinToString(separator = ";") { it -> "\"${it}\"" })
}

the result is

"one";"two";"three";"four";"five";"last"

Upvotes: 0

Shalu T D
Shalu T D

Reputation: 4039

It's simple. You can try like below:-

for (x in "one, two, three...last".split(", ")) {
   println(x)
}

Upvotes: 2

Related Questions