Swift 语言之字符串与字符学习笔记

字符串字面量

1
let someString = "Some string literal value"

初始化空字符串

1
2
3
var emptyString = "" // empty string literal
var anotherEmptyString = String() // initializer syntax
// these two strings are both empty, and are equivalent to each other

判断字符串是否为空:

1
2
3
4
if emptyString.isEmpty {
println("Nothing to see here")
}
// prints "Nothing to see here"

字符串可变性

1
2
3
4
5
6
7
var variableString = "Horse"
variableString += " and carriage"
// variableString is now "Horse and carriage"
let constantString = "Highlander"
constantString += " and another Highlander"
// this reports a compile-time error - a constant string cannot be modified

使用字符

1
2
3
4
let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"]
let catString = String(catCharacters)
println(catString)
// prints "Cat!🐱"

连接字符串和字符

1
2
3
4
5
6
7
8
9
10
11
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome now equals "hello there"
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"

字符串插值

1
2
3
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5"

字面量中的特殊字符(\0, \\, \t, \n, \r, \“, \’)

1
2
3
4
5
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein
let dollarSign = "\u{24}" // $, Unicode scalar U+0024
let blackHeart = "\u{2665}" // ♥, Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // 💖, Unicode scalar U+1F496

计算字符数量

1
2
3
let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
println("unusualMenagerie has \(count(unusualMenagerie)) characters")
// prints "unusualMenagerie has 40 characters"

字符串索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let greeting = "Guten Tag"
// prints "greeting: String = "Guten Tag""
println(greeting.startIndex)
// 0
println(greeting.endIndex)
// 9
greeting[greeting.startIndex.successor()]
// u
greeting[greeting.endIndex.predecessor()]
// g
let index = advance(greeting.startIndex, 7)
greeting[index]
// a
greeting.endIndex.successor() // fatal error: can not increment endIndex

字符串插入和删除

在某个位置插入字符:

1
2
3
4
var welcome = "hello"
welcome.insert("!", atIndex: welcome.endIndex)
println(welcome)
// prints "hello!"

在某个位置插入字符串:

1
2
3
welcome.splice(" there", atIndex: welcome.endIndex.predecessor())
println(welcome)
// prints "hello there!"

删除某个位置的字符:

1
2
3
4
welcome.removeAtIndex(welcome.endIndex.predecessor())
// !
println(welcome)
// prints "hello there"

删除某个范围的字符串:

1
2
3
4
let range = advance(welcome.endIndex, -6)..<welcome.endIndex
welcome.removeRange(range)
println(welcome)
// prints "hello"

字符串和字符比较

1
2
3
4
5
6
let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lot alike, you and I."
if quotation == sameQuotation {
println("These two strings are considered equal")
}
// prints "These two strings are considered equal"

字符串前缀/后缀相等

前缀相等:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
}
}
println("There are \(act1SceneCount) scenes in Act 1")
// prints "There are 5 scenes in Act 1"

后缀相等:

1
2
3
4
5
6
7
8
9
10
11
var mansionCount = 0
var cellCount = 0
for scene in romeoAndJuliet {
if scene.hasSuffix("Capulet's mansion") {
++mansionCount
} else if scene.hasSuffix("Friar Lawrence's cell") {
++cellCount
}
}
println("\(mansionCount) mansion scenes; \(cellCount) cell scenes")
// prints "6 mansion scenes; 2 cell scenes"

UTF-8

1
2
3
4
5
for codeUnit in dogString.utf8 {
print("\(codeUnit) ")
}
print("\n")
// 68 111 103 226 128 188 240 159 144 182

UTF-16

1
2
3
4
5
for codeUnit in dogString.utf16 {
print("\(codeUnit) ")
}
print("\n")
// 68 111 103 8252 55357 56374

Unicode标量

1
2
3
4
5
for scalar in dogString.unicodeScalars {
print("\(scalar.value) ")
}
print("\n")
// 68 111 103 8252 128054
坚持原创技术分享,您的支持将鼓励我继续创作!