Open In App

Scala Iterator addString() method with a separator with example

This method is same as addString() method but here we can even include a separator between all the elements of an Iterator.

Method Definition : def addString(b: StringBuilder, sep: String): StringBuilder
Where, sep is the separator.



Return Type :It returns the elements of an iterator in the String Builder along with the separator stated.

Example #1:




// Scala program of addString()
// method along with separator
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(11, 12, 13, 14)
          
        // Applying addString method
        // with a separator
        val result = iter.addString(new StringBuilder(), "#")
          
        // Displays output
        println(result)
      
    }
}

Output:

11#12#13#14

Example #2:




// Scala program of addString()
// method along with separator
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(5, 6, 9)
          
        // Applying addString method
        // with a separator
        val result = iter.addString(new StringBuilder(), "*")
          
        // Displays output
        println(result)
      
    }
}

Output:
5*6*9

Article Tags :