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

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" 
主站蜘蛛池模板: 岳普湖县| 唐河县| 桃园市| 灵武市| 化州市| 襄城县| 新巴尔虎右旗| 义马市| 宽城| 泰安市| 出国| 紫金县| 繁峙县| 岐山县| 文登市| 六枝特区| 怀安县| 余干县| 武定县| 中江县| 新乐市| 如皋市| 上林县| 聂拉木县| 沁水县| 磐石市| 德惠市| 武川县| 四子王旗| 阿荣旗| 凤城市| 宁蒗| 平谷区| 鲁甸县| 福海县| 宜城市| 洛阳市| 本溪市| 二手房| 嘉义市| 呼图壁县|