Scala List addString() with examples

 164 total views,  1 views today

Welcome to Scala series. In this tutorial, you will learn about the Scala List addString() operation with examples. There is an alternative to addString() operation in Scala, please have a look at mkString()

List() is a collection to store values in an ordered fashion.

addString() is the most useful method of a Scala collection which allows generating a string builder by appending elements of the collection. When addString() is applied on a list or collection, it returns a StringBuilder by concatenation of all elements of the collection. With the help of the String Builder, we can create a String output or do some manipulation.

There are three variants of addString() operation

  1. addString() without separator.
  2. addString() with separator.
  3. addString() with start,separator and end.

Let us see an example of each variant.

addString() – without separator example

This variant of addString() allows us to append all elements of the collection to a string builder.

object AddString {
  def main(args: Array[String]): Unit = {

    //create a List
    var list: List[String] = List("a", "b", "c", "d", "a", "c")

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString
    var newbuilder = list.addString(builder)

    //Print
    println(newbuilder.toString())
  }
}

Output

abcdac

addString() – with separator example

This variant of addString() allows us to append all elements of the collection with a separator to a string builder.

object AddString {
  def main(args: Array[String]): Unit = {

    //create a List
    var list: List[String] = List("a", "b", "c", "d", "a", "c")

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString with separator
    var newbuilder = list.addString(builder, "-")

    //Print
    println(newbuilder.toString())
  }
}

Output

a-b-c-d-a-c

addString() with start,separator and end example

This variant of addString() allows us to append all elements of the collection with a separator and also with a start and end Strings to a string builder.

object AddString {
  def main(args: Array[String]): Unit = {

    //create a List
    var list: List[String] = List("a", "b", "c", "d", "a", "c")

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString with separator,start and end
    var newbuilder = list.addString(builder, "Start", "-", "End")

    //Print
    println(newbuilder.toString())
  }
}

Output

Starta-b-c-d-a-cEnd

ReferencesScala List addString operation

I hope you like this tutorial and you were able to understand Scala list addString operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.