为有牺牲多壮志,敢教日月换新天。

[Swift]遍历字符串

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:MindDraft
➤博主域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9798159.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Swift中无法再使用传统形式的for循环。

1 //传统for循环形式不适用于Swift
2 for(单次表达式;条件表达式;末尾循环体){中间循环体;}

字符串遍历方法1:使用该indices属性可以访问字符串中各个字符的所有索引。

1 let str = "Strengthen!"
2 for 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索引。

 1 extension String {
 2     //subscript函数可以检索数组中的值
 3     //按照索引方式读取字符
 4     subscript (_ i: Int) -> Character {
 5         //读取字符
 6         get {return self[index(startIndex, offsetBy: i)]}
 7     }
 8 }
 9 
10 //方式1
11 let str = "Strengthen!"
12 for num in 0..<str.count
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
20 for num in 0..<str.count
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!"
2 for 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"
2 var strUnicodeScalars = Array(str.utf8)
3 print(strUnicodeScalars)
4 //Prints [83, 116, 114, 101, 110, 103, 116, 104, 101, 110]
5 for 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!"
2 for char in str.reversed() {
3     print("\(char) ", terminator: "")
4 }
5 //Prints ! n e h t g n e r t S 

倒序遍历字符串方法2stride函数 -返回一个任意可变步长类型值的序列。可变步长类型是可以设置偏移量的一维标量。

 1 let str = "Strengthen!"
 2 //from,to,by 最后一个值是 >(或<) to的值
 3 for num in stride(from: str.count-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的值
11 for num in stride(from: str.count-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 = str.components(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 = arrayStr.reduce("", {$0 + $1})
14 let str4 = arrayStr.reduce("-", {$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

最强字符串解析,你想知道,我都有!!!

请点击下方链接继续深入学习!!!不可浅尝辄止!!!

[Swift]字符串根据索引获取指定字符,依据ASCII编码,实现字符和整数的快速转换

[Swift]字符串(String类、NSString类)常用操作

[Swift]扩展String类:extension String

[Swift]ASCII值的获取和转换:extension Character类和extension Int

[Swift]字符串大小写转换,同时实现本地化或设置语言环境

posted @ 2018-10-16 15:15  为敢技术  阅读(8514)  评论(0编辑  收藏  举报