Swift String初始化、定义

优质
小牛编辑
118浏览
2023-12-01

Swift 5.x String初始化、定义

1. 字面量初始化字符串


  • 1.1 单行字面量字符串
let str = "some string"
  • 1.2 字面量初始化多行字符串
let str = """
The white Rabbit put on his spectacles. "Where shell 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."
"""
print(str)

输出结果:

The white Rabbit put on his spectacles. "Where shell 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."

当在多行字符串字面量里面包含了换行, 那个换行符同样会成为字符串里的值.
如果想要使用换行符来让代码易读, 却不想让换行符成为字符串的值, 那就在那些行的末尾使用反斜杠(\)
e.g. 注意The white Rabbit put on his spectacles.后面, 使用了回车换行

let str = """
The white Rabbit put on his spectacles.
"Where shell 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."
"""
print(str)

在多行字符串字面量里面包含了换行符(\n)
e.g. 效果同上

let str = """
The white Rabbit put on his spectacles. \n"Where shell 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."
"""
print(str)

输出结果:

The white Rabbit put on his spectacles. 
"Where shell 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."

为了控制每行的代码长度, 但又不影响多行字符串本身的值和段落样式, 可以在需要折行的位置使用反斜杠(\)
注: 为了避免\连接字符出现的转义误认而报编译错误, 可以通过回车强制换行, 并不会影响字符串的值

e.g.

let str = """
The white Rabbit put on his spectacles. "Where shell \
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."
"""

多行字符串字面量把所有行包含在引号内, 开始和结束默认不会有换行符
要让多行字符串字面量起始或结束于换行, 就在第一或最后一行写一个空行
多行字符串可以缩进以匹配周围的代码. 双引号(""")前的空格会告诉Swift其他行前应该有多少空白是需要忽略的
如果在某行的空格超过了结束的双引号(""")那么这些空格就会被包含

e.g.

let str = """
    This line doesn't begin with whitespace.
        This line begins with your spaces.
    This line doesn't begin with whitespace.
"""
print(str)

输出结果:

    This line doesn't begin with whitespace.
        This line begins with your spaces.
    This line doesn't begin with whitespace.

对比代码注意两端代码, 唯一区别就是最后结尾的(""")前面空格的区别

let str = """
    This line doesn't begin with whitespace.
        This line begins with your spaces.
    This line doesn't begin with whitespace.
    """
print(str)

输出结果:

This line doesn't begin with whitespace.
    This line begins with your spaces.
This line doesn't begin with whitespace.

测试发现, 如果结束的双引号前面的空格超出了全部行中最多空格数量, 编译器会报错

let str = """
    This line doesn't begin with whitespace.
        This line begins with your spaces.
    This line doesn't begin with whitespace.
        """
print(str)

编译器警告:

Insufficient indentation of line in multi-line string literal
Change indentation of this line to match closing delimiter
  • 1.3 字符串里面的特殊字符

转义特殊字符

\0 空字符
\\ 反斜杠
\t 水平制表符
\n 换行符
\r 回车符
\" 双引号
\' 单引号

任意的Uncode标量, 协作\u{n}, 里面的n是一个1-8位的16进制数字, 其值是合法Unicode
可以在多行字符串字面量中包含双引号(")而不需要转义, 要在多行字符穿中包含文本""", 转义至少一个双引号

let dollarSign = "\u{245}"
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
let blackHeart = "\u{2665}"
let sparklingHeart = "\u{1F496}"
print(dollarSign)
print(wiseWords)
print(blackHeart)
print(sparklingHeart)

输出结果:

Ʌ
"Imagination is more important than knowledge" - Einstein
♥

  • 1.4 扩展字符串分隔符 (Raw String) Swift 5新增

在字符串字面量中放置扩展分隔符来在字符串中包含特殊字符而不让他们真的生效
把字符串放在双引号(")内并有井号(#)包裹
如果字符串里面有(")(#)则首尾需要两个(##)
如果需要字符串中某个特殊符号的效果, 使用匹配包裹的井号(#)数量的井号并在前面写转移符号(\)

let str1 = "Line 1\nLine 2"
let str2 = #"Line 1\nLine 2"#
let str3 = #"Line 1\#nLine 2"#
let str4 = ###"Line 1\###nLine 2"###
print("str1: " + str1 + "\n")
print("str2: " + str2 + "\n")
print("str3: " + str3 + "\n")
print("str4: " + str4 + "\n")

输出结果: 我拼接了输出结果, 便于对比

str1: Line 1
Line 2

str2: Line 1\nLine 2

str3: Line 1
Line 2

str4: Line 1
Line 2

2. 初始化器


  • 2.1 初始化器
let str = String()
  • 2.2 通过Character数组来构造
let characters: [Character] = ["C", "a", "t", "!", "\u{23}"]
let catString = String(characters)
print(catString)

输出结果:

Cat!#

下一篇- Swift 5.x 常用Api 汇总