Skip to content

Latest commit

 

History

History
14 lines (14 loc) · 698 Bytes

字符串模板.md

File metadata and controls

14 lines (14 loc) · 698 Bytes

字符串模板

Kotlin 支持在字符串字面值中引用局部变量,只需要在变量名前加上字符 $ 即可,此外还可以包含用花括号括起来的表达式,此时会自动求值并把结果合并到字符串中

    val intValue = 100
    //可以直接包含变量
    println("intValue value is $intValue") //intValue value is 100
    //也可以包含表达式
    println("(intValue + 100) value is ${intValue + 100}")   //(intValue + 100) value is 200

如果你需要在原始字符串中表示字面值($)字符(它不支持反斜杠转义),可以用下列语法:

    val price = "${'$'}100.99"
    println(price)  //$100.99