简体   繁体   English

scala 字符串插值“$”

[英]scala string interpolation for “$”

Why does string interpolation not work when the name of value of '$'?为什么'$'的值名称时字符串插值不起作用?

In the following code, why does the value of $ not get printed?在下面的代码中,为什么没有打印 $ 的值? What is the mistake when the value of x is printed using string interpolation?使用字符串插值打印 x 的值时会出现什么错误?

repl> val x="test value"
repl> val $="some value"
repl> println($)
some value
repl> println(s"value:$x")
value:test value
repl> println(s"value:$$")
value:$

Why is the $ not replaced by its value?为什么 $ 没有被它的价值取代?

To actually print the value of the variable represented by $ , you should enclose it in braces:要实际打印由$表示的变量的值,您应该将其括在大括号中:

println(s"value:${$}")

outputs:输出:

value:some value

Doubling the $ sign does not work because it is used to escape $ itself as explained here .加倍$符号不起作用,因为它用于转义 $ 本身,如此所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM