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

Multi-line String Literals

We can also define multiline string literals without having to use + + for concatenation. Let's create a multiline string in the example 4b_MultilineString.kts:

val name = "Atrium"
val message = """This is an example of
multiline String $name
"""
println(message)

The output is as follows:

Observe the indentation in the preceding example. If we don't want to use indentation, we can put | and use the trimMargin() function to trim the margin as follows:

val name = "Atrium"
val message = """This is an example of
|multiline String $name
"""
println(message.trimMargin())

The output is as follows:

One more thing that we can do is customize the character that we are going to use for margin separation and pass it to the trimMargin() function as follows:

val name = "Atrium"
val message = """This is an example of
^multiline String $name
"""
println(message.trimMargin("^"))

This gives the following output:

主站蜘蛛池模板: 江源县| 新平| 慈溪市| 罗甸县| 九龙县| 怀远县| 凤山市| 中牟县| 吴桥县| 邵东县| 江阴市| 新安县| 留坝县| 九江县| 惠来县| 墨玉县| 崇明县| 武陟县| 防城港市| 呼伦贝尔市| 彝良县| 鹤庆县| 阿瓦提县| 胶南市| 武汉市| 乐山市| 浪卡子县| 修水县| 辽阳市| 抚顺市| 泰顺县| 新巴尔虎右旗| 南安市| 芜湖市| 剑河县| 乃东县| 泸溪县| 望奎县| 师宗县| 商水县| 开鲁县|