Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list in Scala to a formatted string

Tags:

scala

How can I convert the List(1,2,3) in Scala to a formatted string like "1/2/3" with the List methods?

like image 940
Pooya Avatar asked Sep 08 '13 16:09

Pooya


Video Answer


1 Answers

Have a look at mkString. In short:

Displays all elements of this list in a string using a separator string (In your case "/")

scala> List(1,2,3).mkString("/") res0: String = 1/2/3  scala> List(1,2,3).mkString res1: String = 123  // def mkString(start: String,sep: String,end: String): String  scala> List(1,2,3).mkString("@", "/", "@") res2: String = @1/2/3@ 
like image 123
Jatin Avatar answered Sep 22 '22 01:09

Jatin