Reputation: 1
I am trying some string manipulation on sequence
I have sequence of elements ex: Seq("aaa","bbb","ccc") and need to be converted to String with appending some characters to start and end of each element
result should be string -> "aaa","bbb","ccc"
scala> val initial = Seq("aaa","bbb","ccc")
initial: Seq[String] = List(aaa, bbb, ccc)
scala> initial.foldLeft(Seq [String] ()){(z,x)=>z:+("\""+x+"\""+",")}
res2: Seq[String] = List("aaa",, "bbb",, "ccc",)
Any suggestions?
Upvotes: 0
Views: 1171
Reputation: 4243
This can also be achieved using only mkString(start, sep, end)
List("aaa", "bbb", "ccc").mkString("\"", "\",\"", "\"")
Upvotes: 3
Reputation: 130
You don't need a foldLeft to transform elements.
Try this
initial.map(x=> s""""${x}"""").mkString(",")
Upvotes: 1
Reputation: 22840
You probably just want to do this:
def appndAndPrependTo[A](data: Seq[A])(s: String): Seq[String] =
data.map(a => s"${s}${a}${s}")
Upvotes: 1