一、字符串字面量
1、多行字符串
由一对三个双引号包裹着的具有固定顺序的文本字符集。
let quotation = """
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."
"""
2、续行符 和 转义符(\
)
如果你想换行,你可以用在行尾写一个反斜杠(\
)作为续行符。
let quotation = """
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."
"""
字符串里面是可以包含特殊字符:
- 转义字符:
\0
(空字符)、\\
(反斜线)、\t
(水平制表符)、\n
(换行符)、\r
(回车符)、\"
(双引号)、\'
(单引号)。 - Unicode标量:写成
\u{n}
(u 为小写),其中 n 为任意一到八位十六进制数且可用的 Unicode 位码。
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imageination is more important than knowledge" - Enistein
let dollarSign = "\u{24}" // $,Unicode 标量 U+0024
let blackHeart = "\u{2665}" // ♥,Unicode 标量 U+2665
let sparklingHeart = "\u{1F496}" // 💖,Unicode 标量 U+1F496
3、扩展字符串分隔符
#"""
和"""#
下面 两个效果一样
let str3 = #"""
Escaping the first quote \#n
Escaping all three quotes \n
Escaping all three quotes
"""#
let str4 = """
Escaping the first quote \n
Escaping all three quotes \\n
Escaping all three quotes
"""
二 、字符串
1、初始化
下面的初始化是等价的,均为空字符串
var str = ""
var str = String()
字符串多了一个isEmpty
字符串。
if str.isEmpty {
print("no date")
}
2、使用字符串
String
在Swift中是值类型。
可以通过for-in
遍历字符串。
for item in "dog🐶" {
print(item)
}
// D
// o
// g
// 🐶
字符串可以通过传递一个值类型为 Character
的数组作为自变量来初始化:
let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
let catString = String(catCharacters)
print(catString)
// 打印输出:“Cat!🐱”
3、字符串连接
let string1 = "hello"
let string2 = " there"
+
:使用加号来拼接;
var welcome = string1 + string2
// welcome 现在等于 "hello there"
+=
:可以通过赋值运算,将一个字符串 添加到一个已经有值的字符串 变量 上;
var instruction = "look over"
instruction += string2
// instruction 现在等于 "look over there"
多行字符串的拼接;
let start = """
one
two
"""
let start2 = """
one
two
"""
let end = """
three
"""
print(start + end)
// 打印两行:
// one
// twothree
print(start2 + end)
// 打印三行:
// one
// two
// three
三、字符串的访问和修改
1、字符串索引
每个字符串String
都有一个关联索引index
属性。对应着字符串中的每个Character
的位置。
String.index
:对应着字符串 中的每个Character
的位置。
String.startIndex
:获取一个 String 的第一个Character
的索引。
String.endIndex
:获取一个 String 的最后一个Character
的索引。
String
是空串:startIndex
和 endIndex
是相等的。
var str = "Guten Tag!"
print(str)
print("---------字符串索引-----------")
print(str[str.startIndex])
print(str[str.index(before: str.endIndex)])
print(str[str.index(after: str.startIndex)])
print(str[str.index(str.startIndex, offsetBy: 7)])
print(str[str.index(str.endIndex, offsetBy: -7)])
print(str)
print("----------字符串插入----------")
str.insert("🐶", at: str.endIndex)
print(str)
str.insert(contentsOf:" there", at: str.index(before: str.endIndex))
print(str)
print("----------字符串删除----------")
// 这个删除最后的,不能使用str.remove(at: str.endIndex),这样是错误的。
str.remove(at: str.index(before: str.endIndex))
print(str)
// 这里也是不能使用 ...,只能使用..<。
let range = str.index(str.endIndex, offsetBy: -6) ..< str.endIndex
str.removeSubrange(range)
print(str)
// 打印
Guten Tag!
---------字符串索引-----------
G
!
u
a
e
Guten Tag!
----------字符串插入----------
Guten Tag!🐶
Guten Tag! there🐶
----------字符串删除----------
Guten Tag! there
Guten Tag!
2、子字符串
SubString
:和String
有一样的操作,但是只会短时间保存在内存里。如果要长时间保存,就需要把SubString
转化为String
。
print("----------子字符串----------")
let greeting = "Hello, world!"
print(greeting)
let index = greeting.firstIndex(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
print(beginning)
let newString = String(beginning)
print(newString)
// 打印
----------子字符串----------
Hello, world!
Hello
Hello
SubString
是复用了String
的空间,String
需要保存SubString
的空间直到SubString
不被使用为止。看下图理解:
![](https://img.haomeiwen.com/i5892667/d3dfb38bde2fa0ec.png)
3、比较字符串
Swift
提供了三种方式来比较文本(字符串和字符)值:
- 字符串字符相等:(
==
)和(!=
) - 前缀相等:
hasPrefix(_:)
- 后缀相等:
hasSuffix(_:)
网友评论