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

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" 
主站蜘蛛池模板: 庆云县| 乃东县| 成武县| 格尔木市| 玉树县| 延边| 晋江市| 锡林郭勒盟| 江城| 夏邑县| 洮南市| 谢通门县| 合山市| 张家口市| 红河县| 宿松县| 通州市| 九龙县| 库尔勒市| 佳木斯市| 汤阴县| 公安县| 沙河市| 霞浦县| 吉首市| 南和县| 太原市| 舒城县| 庆安县| 浮梁县| 成武县| 佛山市| 上饶市| 张家界市| 永顺县| 嘉定区| 辽阳县| 双江| 竹北市| 晋宁县| 马鞍山市|