user2254180
user2254180

Reputation: 856

Converting Iterator[String] to String

I have an Iterator[String] that I want to convert to a String in Scala. I would expect the following to work, but I get no output. What am I doing wrong?

val it = Iterator("a", "number", "of", "words")
val combined = "";

while (it.hasNext){
   combined = combined + it.next()
}
println(combined)

Upvotes: 3

Views: 5947

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108169

Your code can't even compile, since you're reassigning a val

it.mkString

will do what you're after

Upvotes: 13

Related Questions