duckertito duckertito - 1 year ago 77
Scala Question

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

I have an array in Scala e.g.

and I want to parse it into a string:


How can I do it in Scala?

Answer Source

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?