//String 是一个有序的字符集合, Swift字符串通过String类型表示,也可以表示为Character类型值的集合
var str = "Hello, playground"
//初始化空字符串
var emptyString = ""
var anotherString = String()
//判断字符串是否为空
if emptyString.isEmpty {
print("Nothing to see here")
}
//字符串可变性
var variableString = "Horse"
variableString += " and carriage"
//let constantString = "Highlander"
//constantString += " and Highlander" //会报错
//字符串值类型
//Swift的String类型是值类型,在创建一个新的字符串值,进行常量/变量赋值操作或者在函数/方法中传递时,会进行值拷贝,在不同情况下,都会对已有字符串值创建新的副本,并对该副本进行传递或赋值
//使用字符串-遍历
for character in "Dog!????" {
print(character)
}
//计算字符数量
let unusualMenagerie = "Koala ????, Snail ????, Penguin ????, Dromedary ????"
print("unusualMenagerie has \(unusualMenagerie.count) characters")
//注意
//1.不同的 Unicode 字符以及相同 Unicode 字符的不同表示方式可能需要不同数量的内存空间来存储,所以Swift 中的字符在一个字符串中表示并不一定占用相同的内存空间。因此,字符串的长度不得不通过迭代字符串中每一个字符的长度来进行计算。如果您正在处理一个长字符串,需要注意 countElements 函数必须遍历字符串中的字符,以精准计算字符串的长度。
//2.另外需要注意的是通过 countElements 返回的字符数量并不总是与包含相同字符的 NSString 的 length 属性相同。NSString 的 length 属性是基于利用 UTF-16 表示的十六位code units数目,而不是基于 Unicode 字符。为了解决这个问题,NSString 的 length 属性在被 Swift的 String值访问时会被称为utf16count。
//连接字符串和字符
let string1 = "hello"
let string2 = " here"
let character1 = "!"
let character2 = "?"
let stringPlusCharacter = string1 + character1
let stringPlusString = string1 + string2
let characterPlusString = character1 + string1
let chatacterPlusChatacter = character1 + character2
//加法赋值
var instruction = "look over"
instruction += string2
var welcome = "good moring"
welcome += character1
//字符串插值
let multiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
//注意:插值字符串中写在括号中的表达式不能包含非转义双引号 (") 和反斜杠 (\),并且不能包含回车或换行符。
//比较字符串:字符串相等,前缀相等和后缀相等
//字符串相等
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")
}
//前缀/后缀相等
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 : Int = 0
for scene in romeoAndJuliet {
if scene.hasPrefix("Act 1") {
act1SceneCount += 1
}
}
print("There are \(act1SceneCount) 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")
//大写和小写字符串
let normal = "Could you help me, please?"
let shouty = normal.uppercased()
let whispered = normal.lowercased()
print("\n")
//UTF-8
let dogString = "Dog!????"
for codeUnit in dogString.utf8 {
print("\(codeUnit)")
}
print("\n")
//UTF-16
for codeUnit in dogString.utf16 {
print("\(codeUnit)")
}
print("\n")
//Unicode标量 (Unicode Scalars)
for scalar in dogString.unicodeScalars {
print("\(scalar.value)")
print("\(scalar)")
}
print("\n")