[Swift]遍歷字符串

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/ 
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ylkkrsqb-hx.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Swift中沒法再使用傳統形式的for循環。git

1 //傳統for循環形式不適用於Swift
2 for(單次表達式;條件表達式;末尾循環體){中間循環體;}

字符串遍歷方法1:使用該indices屬性能夠訪問字符串中各個字符的全部索引。github

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的整數值ide

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函數 -返回一個任意可變步長類型值的序列。可變步長類型是能夠設置偏移量的一維標量。post

 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]字符串大小寫轉換,同時實現本地化或設置語言環境

相關文章
相關標籤/搜索