Is there a built in method to take all of the strings in a list of strings and concatenate them in Scala? If not, how would I do this?
2 Answers
You might be looking for mkString:
List("a", "b").mkString //"ab" The method also takes in an argument which joins the elements:
List("a", "b").mkString(" ") //"a b" Without this method, you could use the more primitive reduce:
List("a", "b") reduceLeft { (soFar, next) => soFar + next } List("a", "b").reduceLeft(_+_) Or even the more primitive foldLeft:
List("a", "b").foldLeft("")(_+_) ("" /: List("a", "b"))(_+_)