插值器
- s字符串插值器
- f插值器
- raw插值器
代码
object StringDemo {
//插值器
//s字符串插值器
def main(args: Array[String]): Unit = {
val name = "jerry"
val res = s"hell,$name"
println("s插值器(hell,$name):"+res)
//对$大括号里面的表达式进行运算
val res1 = s"1+1=${1 + 1}"
println("s插值器(${1 + 1}):"+res1)
//f插值器,格式化
val height = 1.9d
val name1 = "tom"
val res2 = f"$name1 is $height%1.2f meter tall "
println("f插值器($name1 is $height%1.2f ):"+res2)
//raw差值器,类似于s差值器
val str = s"a\tb"
val str2 = raw"a\tb"
println("s插值器(a\\tb):"+str)
println("raw插值器(a\\tb):"+str2)
}
}
运行结果
s插值器(hell,$name):hell,jerry
s插值器(${1 + 1}):1+1=2
f插值器($name1 is $height%1.2f ):tom is 1.90 meter tall
s插值器(a\tb):a b
raw插值器(a\tb):a\tb
Process finished with exit code 0