0

So I have the following in Scala:

scala> val example = "hello \tmy \nname \tis \nmaria \tlee".split("\n").map(_.split("\\s+"))
example: Array[Array[String]] = Array(Array(hello, my), Array(name, is), Array(maria, lee))

I want to take each 1-d array and make it into a string, and make an array of these strings (strings should be comma separated). How do I do this?

1 Answer 1

6
scala> example.map(_.mkString)
res0: Array[String] = Array(hellomy, nameis, marialee)

To make the strings comma separated:

scala> example.map(_.mkString(","))
res0: Array[String] = Array(hello,my, name,is, maria,lee)
Sign up to request clarification or add additional context in comments.

2 Comments

what if I want to comma separate the strings?
mkString takes a separator parameter so call mkString with ",". Calling it with no parameter uses a default value which is apparently an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.