Home »
Scala
How to Join an iterable of strings in Scala?
By IncludeHelp Last updated : October 20, 2024
Problem Definition
How to join an iterable of string in Scala?
Problem Description
We will see the method to combine Scala string iterable in Scala.
string = Array("scala", "programming", "language")
Output:
Scala programming language
Here, we need to merge all the elements of the given iterable and store the result in a string separated by some separator (in this case it is space).
You need to create an iterator over the collection to get the values.
Scala program to join an iterable of strings
object MyClass {
def main(args: Array[String]) {
val it = List("scala", "programming", "language")
var joinString = it.mkString(" ")
println("The joined string: " + joinString)
}
}
Output:
The joined string: scala programming language
Reference: Scala program to convert Array to string
Here, we have created a list with string elements and then mkString to join the list of strings. Then printed the value using println method.