The toString
method of List
produces the representation without quotes:
scala> val colorPairs = List("red, green", "yellow, blue")
colorPairs: List[String] = List(red, green, yellow, blue)
scala> colorPairs.toString
res0: String = List(red, green, yellow, blue)
This is implemented in TraversableLike:
/** Converts this $coll to a string.
*
* @return a string representation of this collection. By default this
* string consists of the `stringPrefix` of this $coll, followed
* by all elements separated by commas and enclosed in parentheses.
*/
override def toString = mkString(stringPrefix + "(", ", ", ")")
(Update thanks to som-snytt) However, ScalaRunTime provides various special case rendering, for example:
scala> List("")
res3: List[java.lang.String] = List("")
scala> List("1")
res4: List[java.lang.String] = List(1)
scala> List(" 1 ")
res6: List[java.lang.String] = List(" 1 ")
which is due to the following case
s:
case "" => "\"\""
case x:String => if (x.head.isWhitespace || x.last.isWhitespace) "\"" + x + "\"" else x
There are various ways of showing the list contents more accurately, of course:
scala> colorPairs foreach println
red, green
yellow, blue
scala> colorPairs.map("\"" + _ + "\"")
res2: List[String] = List("red, green", "yellow, blue")
Or you could enrich List and similar collections with a new method for this:
implicit class TLWrapper(t: TraversableLike[String,_]){
def show = t.mkString(t.stringPrefix + "(\"", "\", \"", "\")")
}
scala> List("red, green","yellow, blue").show
res10: String = List("red, green", "yellow, blue")