Convert list in Scala to a formatted string

2020-06-07 05:10发布

问题:

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

回答1:

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
res0: String = 123

// def mkString(start: String,sep: String,end: String): String 
scala> List(1,2,3).mkString("@", "/", "@")
res1: String = @1/2/3@


标签: scala