Open In App

Scala List mkString() method with a separator with example

Improve
Improve
Like Article
Like
Save
Share
Report

The mkString() method is utilized to display all the elements of the list in a string along with a separator.

Method Definition: def mkString(sep: String): String

Return Type: It returns all the elements of the list in a string along with a separator.

Example #1:




// Scala program of mkString()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(2, 3, 5, 7, 8)
          
        // Applying mkString method
        val result = m1.mkString("*")
          
        // Displays output
        println(result)
      
    }


Output:

2*3*5*7*8

Example: 2#




// Scala program of mkString()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a list
        val m1 = List(2, 3, 5, 7, 5)
          
        // Applying mkString method
        val result = m1.mkString("_")
          
        // Displays output
        println(result)
          
    }
  


Output:

2_3_5_7_5


Last Updated : 13 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads