字符串插值,转义引号 [英] String interpolation, escaping quotation mark

查看:67
本文介绍了字符串插值,转义引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事实证明这有多困难,我有点困惑.我已经环顾过 stackoverflow,但似乎没有任何解决方案对我有用.

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.

我想做什么:

val file = checkcache(fileName)

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

现在,对于一个名为blubb"的文件,该文件已经存在于它输出的缓存中,这非常有效

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

找到文件 blubb

并返回文件.

现在我想要这个

找到文件blubb"

所以我尝试这样做:

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

编译器抛出

')' 预期但找到字符串文字.

')' expected but string literal found.

为什么会这样,我如何正确转义双引号,最好在 $file 变量之后或之前没有空格?

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?

推荐答案

使用三引号:

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

这篇关于字符串插值,转义引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆