Escape a dollar sign in string interpolation

0__ picture 0__ · Jun 1, 2013 · Viewed 27.4k times · Source

How do I escape a dollar sign in string interpolation?

def getCompanion(name: String) = Class.forName(s"my.package.$name\$")

// --> "error: unclosed string literal"

Answer

4e6 picture 4e6 · Jun 1, 2013

Just double it

scala> val name = "foo"
name: String = foo

scala> s"my.package.$name$$"
res0: String = my.package.foo$