Swift 3.0学习笔记_4_字符串
2017-04-10 本文已影响11人
henu_Larva
字符串相关
//字符串字面量
let someString = "some string literal value"
//初始化空字符串 下面两种方式等价
var emptyString = ""
var anotherEmptyString = String()
//判断字符串是否为空
if emptyString.isEmpty {
print("Nothing to see here")
}
//字符串可变性:var 修饰的字符串可变, let 修饰的字符串不可变
//字符串是值类型:Swift 的 String 类型是值类型。 如果您创建了一个新的字符串,那么当其进行常量、变量赋值操作,或在函数/ 方法中传递时,会进行值拷贝.任何情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值操作.
//可通过 for-in 的方式获取字符串内的字符
for character in "Dog".characters {
print(character)
}
//通过字符初始化字符串
let catCharacters:[Character] = ["C","a","t"]
let catString = String(catCharacters)
print(catString)
//字符串拼接
//1. +
//2. +=
//3. append
let exclamationMark:Character = "!" //exclamationMark 是字符变量
var welcome = ""
welcome.append(exclamationMark)
//字符串插值
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
//注:插值字符串中写在 括号中 的表达式不能包含非转义反斜杠 ( \ ),并且不能包含回车或换行符。不过,插值字符串可以包含其他字面量。
//计算字符数量
let unusualMenagerie = "Koala ?, Snail ?, Penguin ?, Dromedary ?"
print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")
//访问和修改字符串
//使用startIndex属性可以获取一个String的第一个字符的索引。使用endIndex属性可以获取最后一个字符的 后一个 位置的索引
//通过调用 String 的 index(before:) 或 index(after:)方法,可以立即得到前面或后面的一个索引
let greeting = "Guten Tag!"
greeting[greeting.startIndex] // G
greeting[greeting.index(before: greeting.endIndex)] // !
greeting[greeting.index(after: greeting.startIndex)] // u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index] // a
//使用 characters 的 indices 属性会创建一个包含全部索引的范围 Range, 用来在一个字符串中访问单个字符
for index in greeting.characters.indices {
print("\(greeting[index])",terminator:"")
}
//插入和删除
//调用 insert(_:at:) 方法可以在一个字符串的指定索引插入一个字符,调用 insert(contentsOf:at:) 方法可以在一个字符串的指定索引插入一个段字符串
var new_welcome = "hello"
new_welcome.insert("!", at: new_welcome.endIndex) // hello!
new_welcome.insert(contentsOf: " there".characters, at: new_welcome.index(before: new_welcome.endIndex)) //hello there!
//调用 remove(at:) 方法可以在一个字符串的指定索引删除一个字符,调用 removeSubrange(_:) 方法可以在一个字符串的指定索引删除一个子字符串
new_welcome.remove(at: new_welcome.index(before: new_welcome.endIndex)) //hello there
let range = new_welcome.index(new_welcome.endIndex, offsetBy: -6)..<new_welcome.endIndex
new_welcome .removeSubrange(range) //hello
//注: insert(_:at:), insert(contentsOf:at:), remove(at:), removeSubrange(_:)也可用在 Array, Dictionary, Set 中
//字符串 / 字符 相等
//Swift 提供了三种方式比较文本值:字符串字符相等,前缀相等,后缀相等
let quotation = "We're a lot alike,you and I."
let sameQuotation = "We're a lot alike,you and I."
if quotation == sameQuotation {
print("equal");
}
//前缀 / 后缀 相等
//通过调用字符串的 hasPrefix(_:) / hasSuffix(_:) 方法来检查字符串是否拥有特定前缀 / 后缀,两个方法均接收一个 String 类型的参数,并返回一个布尔值
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
}
}
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
}
}
//注: hasPrefix 和 hasSuffix 方法都是在每个字符串中逐字符比较其可扩展的字符群集是否标准相等