字符串插值:直接在过程字符串文字中嵌入变量引用的机制。只在Scala-2.10及更高版本支持。
提供了三种字符串插值方式:s,f和raw。
1、s 字符串插值器 简单的说就是解析字符串变量。
val name = "Splicing"
println(s"His name is $name")
println(s"His name is ${name}my")
println(s"66 + 88 = ${66+88}")
2、f字符串插值器
文字'f'插值器允许创建一个格式化的字符串,类似于C语言中的printf。 在使用'f'插值器时,所有变量引用都应该是printf样式格式说明符,如%d,%i,%f等。
val hei = 3.141
println(f"The height is $hei%2.2f") // The height is 3.14
println(f"The height is $hei%2.5f") // The height is 3.14100
3、raw插值器
'raw'内插器类似于's'插值器,但是它不执行字符串内的文字转义。
println(s"Result = \n a \n b") //Result =a b
println(raw"Result = \n a \n b")//Result = \n a \n b