[Swift]遍历字符串
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤:MindDraft
➤博主域名:
➤GitHub地址:github/strengthen/LeetCode
➤原⽂地址:
➤如果链接不是⼭青咏芝的博客园地址,则可能是爬取作者的⽂章。
➤原⽂已修改更新!强烈建议点击原⽂地址阅读!⽀持作者!⽀持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Swift中⽆法再使⽤传统形式的for循环。
1//传统for循环形式不适⽤于Swift
2for(单次表达式;条件表达式;末尾循环体){中间循环体;}
字符串遍历⽅法1:使⽤该indices属性可以访问字符串中各个字符的所有索引。
1 let str = "Strengthen!"
2for index in str.indices {
3    print("\(str[index]) ", terminator: "")
4}
5// Prints "S t r e n g t h e n ! "
字符串遍历⽅法2:每个String值都有⼀个关联的索引类型,String.Index它对应Character于字符串中每个值的位置。要访问远离给定索引的索引,可以使⽤该index(_:offsetBy:)⽅法⽽不是多次调⽤其中⼀种⽅法。您可以使⽤下标语法来访问Character特定String索引。
1extension String {
2//subscript函数可以检索数组中的值
3//按照索引⽅式读取字符
4    subscript (_ i: Int) -> Character {
5//读取字符
6get {return self[index(startIndex, offsetBy: i)]}
7    }
8}
9
10//⽅式1
11 let str = "Strengthen!"
12for num in0..&unt
13{
14    let char:Character = str[str.index(str.startIndex,offsetBy: num)]
15      print("\(char) ", terminator: "")
16}
17//Prints "S t r e n g t h e n ! "
18
19//⽅式2
20for num in0..&unt
swift 字符串转数组
21{
22    let char:Character = str[num]
23      print("\(char) ", terminator: "")
24}
25//Prints "S t r e n g t h e n ! "
字符串遍历⽅法3:遍历字符串的每⼀个字符
1 let str = "Strengthen!"
2for char in str.characters {
3    print("\(char) ", terminator: "")
4}
5//Prints "S t r e n g t h e n ! "
字符串遍历⽅法4:遍历字符串的每⼀个字符对应ASCII的整数值
1 let str = "Strengthen"
2var strUnicodeScalars = Array(str.utf8)
3print(strUnicodeScalars)
4//Prints [83, 116, 114, 101, 110, 103, 116, 104, 101, 110]
5for charNum in strUnicodeScalars
6{
7    print("\(charNum) ", terminator: "")
8}
9//Prints "83 116 114 101 110 103 116 104 101 110"
倒序遍历字符串⽅法1:reversed()
1 let str = "Strengthen!"
2for char versed() {
3    print("\(char) ", terminator: "")
4}
5//Prints ! n e h t g n e r t S
倒序遍历字符串⽅法2:stride函数 -返回⼀个任意可变步长类型值的序列。可变步长类型是可以设置偏移量的⼀维标量。
1 let str = "Strengthen!"
2//from,to,by 最后⼀个值是 >(或<) to的值
3for num in stride(from: unt-1,to: 0,by: -1)
4{
5    let char:Character = str[str.index(str.startIndex,offsetBy: num)]
6      print("\(char) ", terminator: "")
7}
8//Prints "! n e h t g n e r t"
9
10//from,through,by 最后⼀个值是 >=(或<=) through的值
11for num in stride(from: unt-1,through: 0,by: -1)
12{
13    let char:Character = str[str.index(str.startIndex,offsetBy: num)]
14      print("\(char) ", terminator: "")
15}
16//Prints "! n e h t g n e r t S "
字符串转字符数组:
1 let str = "I am Strengthen"
2 let array = strponents(separatedBy:"")
3 print("\(array) ", terminator: "")
4//["I", "am", "Strengthen"]
字符数组转字符串:
1/*字符数组转字符串*/
2 let arrayStr = ["s","t","r","e","n","g","t","h","e","n","!"]
3
4//⽅式1
5 let str1 = arrayStr.joined(separator: "")
6 let str2 = arrayStr.joined(separator: "-")
7 print("\(str1) ", terminator: "")
8 print("\(str2) ", terminator: "")
9//strengthen!
10//s-t-r-e-n-g-t-h-e-n-!
11
12//⽅式2:注意与⽅式1⽐较,⽅式2只在⾸位添加"-"
13 let str3 = duce("", {$0 + $1})
14 let str4 = duce("-", {$0 + $1})
15 print("\(str3) ", terminator: "")
16 print("\(str4) ", terminator: "")
17//strengthen!
18//-strengthen!
19
20/*整数数组转字符串*/
21 let arrayInt = [0,1,2,3,4,5,6,7,8,9]
22 let dataInt = arrayInt.map(String.init)
23 let strInt = dataInt.joined(separator: "-")
24 print("\(strInt) ", terminator: "")
25//0-1-2-3-4-5-6-7-8-9
最强字符串解析,你想知道,我都有
请点击下⽅链接继续深⼊学习不可浅尝辄⽌

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。