字符串换行

<aside> 👀 多行字符串使用 \\ 反斜杠来作为续行符,表示该字符串并未换行。

</aside>

let softWrappedQuotation = """
The White Rabbit put on his spectacles.  "Where shall I begin, \\
please your Majesty?" he asked.

"Begin at the beginning," the King said gravely, "and go on \\
till you come to the end; then stop."
"""

多行字符串缩进

<aside> 🔥

多行字符串缩进根据后引号的缩进决定,如下代码所示,因为后引号有 4 个字符的缩进,所以只有第二行文字有缩进,而且其他三行必须跟后引号的缩进保证一致,否则编译不过。

</aside>

let softWrappedQuotation = """
    The White Rabbit put on his spectacles.  "Where shall I begin, \\
        please your Majesty?" he asked.

    "Begin at the beginning," the King said gravely, "and go on \\
    till you come to the end; then stop."
    """

扩展字符串分隔符

<aside> 🗯️ 使用扩展字符串分隔符 # 可忽略字符串中的转义字符。

</aside>

let threeMoreDoubleQuotationMarks = #"""
Here are three more double quotes: """ \\n
"""#

// Prints: Here are three more double quotes: """ \\n

<aside> 💨 使用扩展字符串分隔符 # 忽略转义字符的字符串中,若不想被忽略,可在转义字符的 \\ 后加一个 #

</aside>

print(#"6 times 7 is \\#(6 * 7)."#)
// 打印 "6 times 7 is 42."

字符数组→字符串的转化

<aside> 💭 将字符数组转化为字符串:

</aside>

let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
let catString = String(catCharacters)
print(catString)
// 打印输出:“Cat!🐱”

字符串结尾