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

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" 
主站蜘蛛池模板: 兴海县| 桂林市| 靖远县| 长丰县| 翼城县| 商水县| 游戏| 鄯善县| 丹凤县| 五莲县| 宜兰县| 芮城县| 淮北市| 华宁县| 湘西| 宁城县| 永丰县| 新野县| 南部县| 连山| 河曲县| 方正县| 喀喇| 读书| 河间市| 婺源县| 汉川市| 武清区| 清水河县| 东辽县| 康保县| 康乐县| 开江县| 平昌县| 望都县| 枞阳县| 东台市| 镇江市| 延长县| 新丰县| 峡江县|