按照swift教程的内容,把自己觉得重要的记录了下来。——新波
2.1字符串字面量String Literals
字符串字面量是由双引号 ( "" ) 包裹着的具有固定顺序的文本字符集。
let someString = "Some string literal value"
2.2初始化空字符串 Initializing an Empty String
var emptyString = "" // 空字符串字面量
var anotherEmptyString = String() // 构造方法
// 两个字符串均为空并等价。
2.3字符串可变性 String Mutability
(用let)赋值给常量的字符串不能再修改。
2.4字符串是值类型Strings Are Value Types
由于字符串是值类型,赋值、复制、传递过程中,不会对原字符串产生影响,而是创建一个新副本,并对该新副本进行传递或赋值操作。
2.5使用字符Working with Characters
通过标明一个 Character 类型并用字符字面量进行赋值,
let exclamationMark: Character = "!"
let catCharacters: [Character] = ["C", "a", "t", "!", "??"]
let catString = String(catCharacters)
print(catString)
// Prints "Cat!??"
2.6连接字符串和字符 Concatenating Strings and Characters
let string1 = "hello"
let string2 = " there"
var welcome = string1 + string2
// welcome 现在等于 "hello there"
var instruction = "look over"
instruction += string2
// instruction 现在等于 "look over there"
let exclamationMark: Character = "!
welcome.append(exclamationMark)
// welcome 现在等于 "hello there!"
2.7 字符串插值 (String Interpolation)
字符串插值是一种构建新字符串的方式,可以在其中包含常量、变量、字面量和表达式。您插入的字符串字面量的每一项都在以反斜线为前缀的圆括号中:
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message is "3 times 2.5 is 7.5
2.8 Unicode
Unicode 标量(Unicode Scalars)
Swift 的 String 类型是基于 Unicode 标量建立的。 Unicode 标量是对应字符或者修饰符的唯一的21位数字,例如 U+0061 表示小写的拉丁字母( LATIN SMALL LETTER A )(" a "), U+1F425 表示小鸡表情( FRONT-FACING BABY CHICK ) (" ? ")。
字符串字面量的特殊字符 (Special Characters in String Literals)
字符串字面量可以包含以下特殊字符:
• 转义字符 \0 (空字符)、 \\ (反斜线)、 \t (水平制表符)、 \n (换行符)、 \r (回车符)、 \" (双引号)、 \‘ (单引号)。
• Unicode 标量,写成 \u{n} (u为小写),其中 n 为任意一到八位十六进制数且可用的 Unicode 位码。
可扩展的字形群集(Extended Grapheme Clusters)
let eAcute: Character = "\u{E9}" // é
let combinedEAcute: Character = "\u{65}\u{301}" // e followed by ?
// eAcute is é, combinedEAcute is e?
2.9计算字符数量 Counting Characters
如果想要获得一个字符串中 Character的数量,可以使用字符串的 characters 属性的 count 属性:
注意: 可扩展的字符群集可以组成一个或者多个 Unicode 标量。
NSString 的 length属性是利用UTF-16 表示的十六位代码单元数字。
var word = "cafe"
print("the number of characters in \(word) is \(word.characters.count)")
// Prints "the number of characters in cafe is 4"
word += "\u{301}" // COMBINING ACUTE ACCENT, U+0301
print("the number of characters in \(word) is \(word.characters.count)")
// Prints "the number of characters in cafe? is 4"
2.10访问和修改字符串 Accessing and Modifying a String
字符串索引 String Indices
每一个String值都有一个关联的索引类型,String.Index,它对应着字符串中的每一个Character的位置。
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.endIndex.predecessor()]
// !
greeting[greeting.startIndex.successor()]
// u
let index = greeting.startIndex.advancedBy(7)
greeting[index]
// a
greeting[greeting.endIndex] // error
greeting.endIndex.successor() // error
for index in greeting.characters.indices {
print("\(greeting[index]) ", terminator: "")
}
// prints "G u t e n T a g ! "
插入和删除 Inserting and Removing
insert(_:atIndex:) ,insertContentsOf(_:at:),removeAtIndex(_:)
var welcome = "hello"
welcome.insert("!", atIndex: welcome.endIndex)
// welcome now equals "hello!"
welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor())
// welcome now equals "hello there!"
welcome.removeAtIndex(welcome.endIndex.predecessor())
// welcome now equals "hello there"
To remove a substring at a specified range, use the removeRange(_:) method:
let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex
welcome.removeRange(range)
// welcome now equals "hello"
2.11比较字符串 (Comparing Strings)
字符串/字符相等 (String and Character Equality)
等于( == )和不等于( != )
let quotation = "We‘re a lot alike, you and I."
let sameQuotation = "We‘re a lot alike, you and I."
if quotation == sameQuotation {
print("These two strings are considered equal")
}
// 打印输出 "These two strings are considered equal"
// "Voulez-vous un café?" using LATIN SMALL LETTER E WITH ACUTE
let eAcuteQuestion = "Voulez-vous un caf\u{E9}?"
// "Voulez-vous un cafe??" using LATIN SMALL LETTER E and COMBINING ACUTE ACCENT
let combinedEAcuteQuestion = "Voulez-vous un caf\u{65}\u{301}?"
if eAcuteQuestion == combinedEAcuteQuestion {
print("These two strings are considered equal")
}
前缀/后缀相等 (Prefix and Suffix Equality)
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"
]
您可以调用 hasPrefix(_:) 方法来计算话剧中第一幕的场景数:
var act1SceneCount = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1 ") {
act1SceneCount += 1
}
}
print("There are \(act1SceneCount) scenes in Act 1")
// Prints "There are 5 scenes in Act 1"
相似地,您可以用 hasSuffix(_:) 方法来计算发生在不同地方的场景数:
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")
// Prints "6 mansion scenes; 2 cell scenes"
字符串的 Unicode 表示形式(Unicode Representations of Strings)
当一个 Unicode 字符串被写进文本文件或者其他储存时,字符串中的 Unicode 标量会用 Unicode 定义的几种编码格式编码。每一个字符串中的小块编码都被称为代码单元。编码格式包括 UTF-8 编码格式(编码字符串为8位的代码单元), UTF-16 编码格式(编码字符串为16位的代码单元),以及 UTF-32 编码格式(编码字符串为32位的代码单元)。
• UTF-8 代码单元集合 (利用字符串的 utf8 属性进行访问)
• UTF-16 代码单元集合 (利用字符串的 utf16 属性进行访问)
• 21位的 Unicode 标量值集合,也就是字符串的 UTF-32 编码格式 (利用字符串的 unicodeScalars 属性进行访问)
let dogString = "Dog???"(U+1F436)
UTF-8 Representation
for codeUnit in dogString.utf8 {
print("\(codeUnit) ", terminator: "")
}
print("")
// 68 111 103 226 128 188 240 159 144 182
UTF-16 Representation
for codeUnit in dogString.utf16 {
print("\(codeUnit) ", terminator: "")
}
print("")
// 68 111 103 8252 55357 56374
Unicode Scalar Representation
for scalar in dogString.unicodeScalars {
print("\(scalar.value) ", terminator: "")
}
print("")
// 68 111 103 8252 128054
for scalar in dogString.unicodeScalars {
print("\(scalar) ")
}
// D // o // g // ? // ??