138

如何在字符串插值中转义美元符号?

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

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

1 回答 1

203

加倍就行了

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

scala> s"my.package.$name$$"
res0: String = my.package.foo$
于 2013-06-01T18:01:32.213 回答