Swift — String的索引、遍历
一、基于EnumeratedSequence
的遍历
//字符串的遍历
let randomString = "hello everybody"
//基于EnumeratedSequence的遍历
for (i, char) in randomString.enumerated() {
print("The results of the ergodic\(i): \(char)")
/*打印输出:
The results of the ergodic0: h
The results of the ergodic1: e
The results of the ergodic2: l
The results of the ergodic3: l
The results of the ergodic4: o
The results of the ergodic5:
The results of the ergodic6: e
The results of the ergodic7: v
The results of the ergodic8: e
The results of the ergodic9: r
The results of the ergodic10: y
The results of the ergodic11: b
The results of the ergodic12: o
The results of the ergodic13: d
The results of the ergodic14: y
*/
二、for in
正序遍历
//正序
let randomString = "hello everybody"
for char in randomString{
print("The results of the ergodic:\(char)")
/*打印输出:
The results of the ergodic:h
The results of the ergodic:e
The results of the ergodic:l
The results of the ergodic:l
The results of the ergodic:o
The results of the ergodic:
The results of the ergodic:e
The results of the ergodic:v
The results of the ergodic:e
The results of the ergodic:r
The results of the ergodic:y
The results of the ergodic:b
The results of the ergodic:o
The results of the ergodic:d
The results of the ergodic:y*/
}
三、for in
逆序遍历
//逆序
let randomString = "hello everybody"
var index = randomString.count
for char in randomString.reversed() {
index -= 1
print("The results of the ergodic:\(index)=\(char)")
/*打印输出:
The results of the ergodic:14=y
The results of the ergodic:13=d
The results of the ergodic:12=o
The results of the ergodic:11=b
The results of the ergodic:10=y
The results of the ergodic:9=r
The results of the ergodic:8=e
The results of the ergodic:7=v
The results of the ergodic:6=e
The results of the ergodic:5=
The results of the ergodic:4=o
The results of the ergodic:3=l
The results of the ergodic:2=l
The results of the ergodic:1=e
The results of the ergodic:0=h
*/
}
字符串索引
在讲解String基于索引的遍历前先扩展一下 字符串的索引,那么在提到字符串的索引前就要先认识一下
字形群集
1.可扩展的字符群集可以组成一个或者多个 Unicode 标量。这意味着
不同的字符以及相同字符的不同表示方式可能需要不同数量的内存空间来存储
。所以 Swift 中的字符在一个字符串中并不一定占用相同的内存空间数量。因此在没有获取字符串的可扩展的字符群的范围时候,就不能计算出字符串的字符数量
。如果您正在处理一个长字符串,需要注意characters属性必须遍历全部的 Unicode 标量,来确定字符串的字符数量
。2.另外需要注意的是通过characters属性返回的字符数量并不总是与包含相同字符的NSString的length属性相同。
NSString的length属性是利用 UTF-16 表示的十六位代码单元数字,而不是 Unicode 可扩展的字符群集
。3.前面提到,不同的字符可能会占用不同数量的内存空间,所以
要知道Character的确定位置,就必须从String开头遍历每一个 Unicode 标量直到结尾
。因此,Swift 的字符串不能用整数(integer)做索引
。
如下的字符:
//字符串字面量的特殊字符
let dollarSign = "\u{24}" // $, Unicode 标量 U+0024
let blackHeart = "\u{2665}" // ♥, Unicode 标量 U+2665
let sparklingHeart = "\u{1F496}" // 💖, Unicode 标量 U+1F496
print(dollarSign,blackHeart,sparklingHeart)//打印 $ ♥ 💖
//可扩展的字形群集
let eAcute: Character = "\u{E9}" // é
let combinedEAcute: Character = "\u{65}\u{301}" // e 后面加上 ́
// eAcute 是 é, combinedEAcute 是 é
print("eAcuteValue:\(eAcute),combinedEAcuteValue:\(combinedEAcute)")//打印 eAcuteValue:é,combinedEAcuteValue:é
索引
1.使用
startIndex
属性可以获取一个String的第一个Character的索引。使用endIndex
属性可以获取最后一个Character的后一个位置的索引。因此,endIndex属性不能作为一个字符串的有效下标
。如果String是空串,startIndex和endIndex是相等的
。2.通过调用 String 的
index(before:)
或index(after:)
方法,可以立即得到前面或后面的一个索引。您还可以通过调用index(_:offsetBy:) 方法来获取对应偏移量的索引
,这种方式可以避免多次调用 index(before:) 或 index(after:) 方法
。
let stringIndexStr = "hello word!"
//获取字符串第一个索引的 字符
let starIndexC = stringIndexStr[stringIndexStr.startIndex]//获取字符串第一个索引的 字符
print("startIndexValue:\(stringIndexStr.startIndex)")//打印: startIndexValue:Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)
print("starIndexCharacter:\(starIndexC)")//打印:starIndexCharacter:h
//获取字符串最后一个索引的 字符
//错误方法
//endIndex属性不能作为一个字符串的有效下标,运行时会崩溃
//let endIndexC = stringIndexStr[stringIndexStr.endIndex]//试图获取越界索引对应的 Character,将引发一个运行时错误。
//print("endIndexCharacter:\(endIndexC)")
//正确方法
let endIndexC = stringIndexStr[stringIndexStr.index(before: stringIndexStr.endIndex)]//试图获取越界索引对应的 Character,将引发一个运行时错误。
print("endIndexCharacter:\(endIndexC)")//打印:endIndexCharacter:!
//获取字符串 第二个索引的字符(第一个索引后面的一个索引值)
let afterIndexC = stringIndexStr[stringIndexStr.index(after: stringIndexStr.startIndex)]//获取字符串 第二个索引的字符(第一个索引后面的一个索引值)
print(afterIndexC)//打印:e
//获取字符串末尾的一个字符
let beforeIndexC = stringIndexStr[stringIndexStr.index(before: stringIndexStr.endIndex)]//获取字符串末尾的一个字符(最后一个索引前面的一个索引,使用endIndex属性可以获取最后一个Character的后一个位置的索引,endIndex属性不能作为一个字符串的有效下标)
print("获取字符串末尾的一个字符:\(beforeIndexC)")//打印:获取字符串末尾的一个字符: !
//第一个索引4个偏移量之后的一个索引
let offSetByIndex = stringIndexStr.index(stringIndexStr.startIndex, offsetBy: 4)//第一个索引4个偏移量之后的一个索引
print(offSetByIndex,stringIndexStr[offSetByIndex])//打印:Index(_base: Swift.String.UnicodeScalarView.Index(_position: 4), _countUTF16: 1) o
基于索引的遍历
四、基于索引的正序遍历
//基于索引的正序遍历
let randomString = "hello everybody"
for i in 0..<randomString.count {
let char: Character = randomString[randomString.index(randomString.startIndex, offsetBy: i)]
print("swift-string ergodic\(i): \(char)")
/*打印输出:
swift-string ergodic0: h
swift-string ergodic1: e
swift-string ergodic2: l
swift-string ergodic3: l
swift-string ergodic4: o
swift-string ergodic5:
swift-string ergodic6: e
swift-string ergodic7: v
swift-string ergodic8: e
swift-string ergodic9: r
swift-string ergodic10: y
swift-string ergodic11: b
swift-string ergodic12: o
swift-string ergodic13: d
swift-string ergodic14: y
*/
}
五、基于索引的逆序遍历
//基于索引的逆序遍历
let randomString = "hello everybody"
for i in stride(from: randomString.count - 1, through: 0, by: -1) {
let char: Character = randomString[randomString.index(randomString.startIndex, offsetBy: i)]
print("swift-string ergodic\(i): \(char)")
/*打印输出:
swift-string ergodic14: y
swift-string ergodic13: d
swift-string ergodic12: o
swift-string ergodic11: b
swift-string ergodic10: y
swift-string ergodic9: r
swift-string ergodic8: e
swift-string ergodic7: v
swift-string ergodic6: e
swift-string ergodic5:
swift-string ergodic4: o
swift-string ergodic3: l
swift-string ergodic2: l
swift-string ergodic1: e
swift-string ergodic0: h
*/
}
以上五种Swift中String的遍历方法总有一款用得到
网友评论