How to Join an iterable of strings in Scala?

Here, we will learn to join an iterable of string in Scala?
Submitted by Shivang Yadav, on December 30, 2020

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.

Program:

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.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.