I have two strings:
val s1: String = "aaa/$Y/$m/aaa_$Y$m$d" // this one has all three variables
val s2: String = "aaa/$Y/$m" // this one has only two variables
val myStrings: Seq[String] = Seq(s1, s2)
val myStringsUpdated: Seq[String] = myStrings.map(s => ???)
Can I inject Y
, m
, d
values into s1
, s2
dynamically so I get myStringsUpdated
sequence?
As Tim already mentioned interpolation is done at compile-time, so you can't use it directly, but you could do a simple trick and wrap it into function:
//instead of plain interpolated string we've got function returning interpolated string
def s1(Y: String, m: String, d: String): String = s"aaa/$Y/$m/aaa_$Y$m$d"
def s2(Y: String, m: String, d: String): String = s"aaa/$Y/$m"
val myStrings: Seq[(String, String, String) => String] = Seq(s1, s2)
//you inject value by just applying function
val myStringsUpdated = myStrings.map(_.apply("1999", "11", "01"))
println(myStringsUpdated) //List(aaa/1999/11/aaa_19991101, aaa/1999/11)