duckertito duckertito - 26 days ago 7
Scala Question

How to flatten an array and convert it into a string?

I have an array in Scala e.g.

Array("11","112","as2")
and I want to parse it into a string:

"['11','112','as2']"


How can I do it in Scala?

Answer

mkString is one way to go. The scala REPL is great for this sort of thing:

scala> Array("11","112","as2")
res0: Array[String] = Array(11, 112, as2)

scala> "['"+res0.mkString("','")+"']"
res1: String = ['11','112','as2']

But if you are creating JSON, perhaps something from your JSON library would be more appropriate?