Question

How do I escape a dollar sign in string interpolation?

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

// --> "error: unclosed string literal"
Was it helpful?

Solution

Just double it

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

scala> s"my.package.$name$$"
res0: String = my.package.foo$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top