String interpolation, escaping quotation mark

萝らか妹 提交于 2019-11-28 04:31:07

问题


I'm somewhat baffled by how difficult this turns out to be. I've already looked around stackoverflow, but no solution seems to work fine for me.

What I want to do:

val file = checkcache(fileName)

file match
{
    case Some(_) => {println(s"File $file found!"); file.get}
    case None => createFile(fileName)
}

Now, this works perfectly fine, for a file named "blubb" that already resides in the cache it outprints

File blubb found

and returns the file.

Now I want this to be

File "blubb" found

So I tried doing this:

case Some(_) => { println(s"File \" $file \" found!"); file.get}

Compiler throws

')' expected but string literal found.

Why is that and how do I escape a double quotation mark correctly and preferably without an empty space after or before the $file-variable?


回答1:


Use triple quotation mark:

scala> s"""File "$file" found!"""
res0: String = File "blubb" found!


来源:https://stackoverflow.com/questions/31366563/string-interpolation-escaping-quotation-mark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!