官术网_书友最值得收藏!

String templates

String templates are a simple and effective way of embedding values, variables, or even expressions inside a string without the need for pattern replacement or string concatenation. Many languages now support this kind of feature, and Kotlin's designers also opted to include it (you might see the technique referred to in the Kotlin context as string interpolation).

String templates improve on the Java experience when using multiple variables in a single literal, as it keeps the string short and more readable.

Java developers will be familiar with the usage of string concatenation to mix expressions with string literals:

    val name = "Sam" 
    val concat = "hello " + name 

Usage is extremely straightforward. A value or variable can be embedded simply by prefixing with a dollar ($) symbol:

    val name = "Sam" 
    val str = "hello $name" 

Arbitrary expressions can be embedded by prefixing with a dollar ($) symbol and wrapping, in braces ({}):

    val name = "Sam" 
    val str = "hello $name. Your name has ${name.length} characters" 
主站蜘蛛池模板: 阳新县| 庄河市| 武汉市| 石家庄市| 屏山县| 华宁县| 工布江达县| 怀化市| 资兴市| 宣恩县| 宁陵县| 文昌市| 日照市| 九台市| 乐亭县| 卓资县| 登封市| 衡水市| 衡水市| 聂拉木县| 西盟| 巴彦县| 西盟| 桂林市| 民权县| 兴安盟| 萨迦县| 武平县| 资溪县| 东乡| 沂水县| 洪雅县| 天祝| 重庆市| 扎赉特旗| 神木县| 长汀县| 白玉县| 上虞市| 五峰| 顺昌县|