1.字符串字面量
let someString = "Some string literal value"// Swift推断为String类型
2.初始化空字符串
var emptyString = ""// 空字符串字面量
var anotherEmptyString = String()
可以使用字符串的.isEmpty属性来判断是否为空
3.字符串可变性
var str = "Sun Wukong"
str += " is the monkey king"
// str = "Sun Wukong is the monkey king"
// 切记 let 声明的字符串不可修改
4.使用字符
var str:String = "Hello你好"
1>字符串长度
print(str.characters.count)
2>字节长度
print(str.lengthOfBytes(using: .utf8))
3>使用字符
for a in str.characters{
print("字符",a)
}
let catCharacters: [Character] = ["C", "a", "t", "!", "?"]
let catString = String(catCharacters)
// catString = "Cat!?"
5.连接字符串和字符
let string1 = "Hello"
let string2 = " there"
var welcome = string1 + string2
print("加法拼接字符串", welcome)
// "Hello there"
let name: String? = "老王"
let age = 80
let location = "隔壁"
print("不同类型拼接字符串", location + (name ?? "a") + String(age) + "岁")
// 隔壁老王80岁
print("\(location)\(name)\(age)岁")
// 隔壁Optional("老王")80岁 !
var instruction = "look over"
instruction += string2
print("使用+=拼接字符串", instruction)
// look over there
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
print("使用append添加到字符串未", welcome)
// Hello there!
6.格式字符串
let h = 13
let m = 5
let s = 9
let timeStr = String(format: "%02d:%02d:%02d", arguments: [h,m,s])
let timeStr1 = String(format: "%02d:%02d:%02d", h,m,s)
// timeStr = "13:05:09"
// timeStr1 = "13:05:09"
7.字符串插值
字符串插值是一种构建新字符串的方式,可以在其中包含常量、变量、字面量和表达式。 插入的字符串字面量 的每一项都在以反斜线为前缀的圆括号中:
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message 是 "3 times 2.5 is 7.5"
8.Unicode
Unicode是一个国际标准,用于文本的编码和表示。它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符
进行读写操作。 Swift 的 String 和 Character 类型是完 全兼容 Unicode 标准的。
9.访问字符串
let greeting = "Guten Tag!"
// 第一个字符 "G"
let greetingStart: Character = greeting[greeting.startIndex]
// 最后一个字符 "!"
let greetingBefore: Character = greeting[greeting.index(before: greeting.endIndex)]
// 第一个字符后面一个字符 "u"
let greetingAfter: Character = greeting[greeting.index(after: greeting.startIndex)]
// 取第七位的字符 "a" (首位为0位)
let index = greeting.index(greeting.startIndex, offsetBy: 7)
let greetingIndex = greeting[index]
// endIndex可以获取最后一个字符的后一个位置的索引
// greeting[greeting.endIndex] // error 错误❌
// greeting.index(after: endIndex) // error 错误❌
10.修改字符串
var come = "hello"
come.insert("!", at: come.endIndex)
print("插入一个字符", come)
come.insert(contentsOf: " there".characters, at: come.index(before: come.endIndex))
print("插入一个字符串", come)
come.remove(at: come.index(before: come.endIndex))
print("删除一个字符", come)
let range = come.index(come.endIndex, offsetBy: -6)..<come.endIndex
come.removeSubrange(range)
print("删除一个字符串", come)
come.removeAll()
print("删除全部", come)
11.比较字符串
1>相等 ==
2>前缀相等 hasPrefix(_:)
3>后缀相等 hasSuffix(_:)
let romeoAndJuliet = [
"Act 1 Scene 1: Verona, A public place",
"Act 1 Scene 2: Capulet's mansion",
"Act 1 Scene 3: A room in Capulet's mansion",
"Act 1 Scene 4: A street outside Capulet's mansion",
"Act 1 Scene 5: The Great Hall in Capulet's mansion",
"Act 2 Scene 1: Outside Capulet's mansion",
"Act 2 Scene 2: Capulet's orchard",
"Act 2 Scene 3: Outside Friar Lawrence's cell",
"Act 2 Scene 4: A street in Verona",
"Act 2 Scene 5: Capulet's mansion",
"Act 2 Scene 6: Friar Lawrence's cell"
]
// 前缀
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1") {
act1SceneCount += 1
}
}
print("There are \(act1SceneCount) scenes in Act 1")
// There are 5 scenes in Act 1
// 后缀
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
if scene.hasSuffix("Capulet's mansion"){
mansionCount += 1
} else if scene.hasSuffix("Friar Lawrence's cell") {
cellCount += 1
}
}
print("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
// 6 mansion scenes;2 cell scenes