1

Assume I want to create a static string, I can do this like s"""123""", but if 123 is already defined in the variable [result]

val result = "123"

I don't know that is there a syntax to combine s (interpolator) with the string variable or interpolator is only for static text?

Howli
  • 12,291
  • 19
  • 47
  • 72
Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53

1 Answers1

3

The whole point of string interpolation is to do just that.

scala> val result = "123"
result: String = 123

scala> s"this -> $result"
res0: String = this -> 123
dhg
  • 52,383
  • 8
  • 123
  • 144
  • 1
    +1 for the great dignity with which this question was addressed. See also http://stackoverflow.com/q/7917462/1296806 and http://stackoverflow.com/q/22643036/1296806 – som-snytt Apr 29 '14 at 03:21
  • We might further blow the questioner's mind by pointing out that an infinite number of different interpolation schemes are admitted by the Scala 2.10 string interpolation mechanism. … As long as you can work with `$foo` or `${foo.bar.glorch(23, fleeb)}` (etc.). – Randall Schulz Apr 29 '14 at 04:13
  • 1
    Personally I even prefer ${foo} to $foo because for _grep_ and the likes, `${` is more likely to find interpolations, whereas `$` can be just a dollar sign in a string (for instance a price in USD). – eruve Apr 29 '14 at 05:41
  • The bit about "… as long as you can work with …" was to point out that while what you do in the interpolation process is completely open, the syntax within the interpolated string is fixed. Just try generating some code for one of those languages that's in love with the almighty `$`… – Randall Schulz Apr 29 '14 at 13:56