ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.