Bob Reynolds Bob Reynolds - 8 days ago 5
Scala Question

Insert tokens into a string dynamicaly Scala

I have a list of Array[String] that i want to transform into a list of String like this :

val sqlMap ={case x => "INSERT INTO bddMedic VALUES ('"+x(0)+"','"+x(1)+"','"+x(2)+"','"+x(3)+"','"+x(4)+"','"+x(5)+"');"})

So i could generate my SQL request.
My problem is that i hardcode the number of element in my Array but the things is that sometimes there are 6 or 7 element in the array.

What i want is a code that would give me the String

"INSERT INTO bddMedic VALUES ('"+x(0)+"','"+x(1)+"','"+x(2)+"','"+x(3)+"','"+x(4)+"','"+x(5)+"');"

(with the x(0) replaced by the value)
but without having to specify the x(0) x(1) x(2).
I realy have no idea on how to do it and every try is a failure.


Try using mkString:

scala> val x = Array(1,2,3,4,5)
x: Array[Int] = Array(1, 2, 3, 4, 5)

scala> x.mkString(",")
res2: String = 1,2,3,4,5