1. 程式人生 > >[Swift]擴展String類:extension String

[Swift]擴展String類:extension String

class .cn 表達式 cnblogs 進制 sci pat 首字符 ons

請參考本博客另一篇技術博文:《[Swift]字符串(String類、NSString類)常用操作》

  1 extension String {
  2     
  3     //獲取字符串首字符
  4     var first: String 
  5     {
  6         get
  7         {
  8             return String(self[startIndex])
  9         } 
 10     }
 11     
 12     // 獲取字符串最後一個字符
 13     var last: String 
14 { 15 get 16 { 17 let index = self.index(before: self.endIndex) 18 return String(self[index ]) 19 } 20 } 21 22 // 將16進制字符串轉為Int 23 var hexInt:Int 24 { 25 get 26 { 27 return
Int(self, radix: 16) ?? 0 28 } 29 } 30 31 //獲取指定索引位置的字符,返回為字符串形式 32 func charAt(_ num:Int) -> String 33 { 34 guard num < self.count else { 35 assertionFailure("Index out of range!") 36 return String() 37 } 38
let index = slef.index(str.startIndex,offsetBy: num) 39 return String(self[index]) 40 } 41 42 //獲取指定索引位置字符的ASCII整數值 43 func toInt(_ num:Int) -> String 44 { 45 guard num < self.count else { 46 assertionFailure("Index out of range!") 47 return String() 48 } 49 var number:Int = Int() 50 for scalar in charAt(num).unicodeScalars 51 { 52 number = Int(scalar.value) 53 } 54 return number 55 } 56 57 //獲取重復指定次數的字符串 58 func repeat(_ times: Int ) -> String 59 { 60 var result = String() 61 for i in 0..times { 62 result += self 63 } 64 return result 65 } 66 67 //整體反轉字符串 68 func reverse() -> String 69 { 70 return String(slef.reversed()) 71 } 72 73 // 截取字符串:從起始處到index 74 // - Parameter index: 結束索引 75 // - Returns: 子字符串 76 func subStringTo(_ index: Int) -> String { 77 guard index < self.count else { 78 assertionFailure("Index out of range!") 79 return String() 80 } 81 let index = self.index(self.startIndex, offsetBy: index) 82 return String(self[startIndex...index]) 83 } 84 85 // 截取字符串:從index到結束處 86 // - Parameter index: 開始索引 87 // - Returns: 子字符串 88 func subStringFrom(_ index: Int) -> String { 89 90 guard index < self.count else { 91 assertionFailure("Index out of range!") 92 return String() 93 } 94 guard index >= 0 else { 95 assertionFailure("index can‘t be lower than 0") 96 return "" 97 } 98 let index = self.index(self.endIndex, offsetBy: index - self.count) 99 100 return String(self[index..<endIndex]) 101 } 102 103 // 截取字符串:指定區間 104 // - Parameter range: 閉區間 105 // - Returns: 子字符串 106 func subString(_ range: CountableClosedRange<Int>) -> String { 107 108 guard range.lowerBound >= 0 else { 109 assertionFailure("lowerBound of the Range can‘t be lower than 0") 110 return String() 111 } 112 guard range.upperBound < self.count else { 113 assertionFailure("upperBound of the Range beyound the length of the string") 114 return String() 115 } 116 let start = self.index(self.startIndex, offsetBy: range.lowerBound) 117 let end = self.index(self.startIndex, offsetBy: range.upperBound + 1) 118 return String(self[start..<end]) 119 } 120 121 // 隱藏手機號中間字符 122 mutating func hidePhoneNum() { 123 if self.count != 11 { 124 return ; 125 } 126 let start = self.index(self.startIndex, offsetBy: 3) 127 let end = self.index(self.endIndex, offsetBy: -4) 128 let range = Range.init(uncheckedBounds: (lower: start, upper: end)) 129 self.replaceSubrange(range, with: "****") 130 } 131 132 //使用正則表達式替換 133 func pregReplace(pattern: String, with: String,options: NSRegularExpression.Options = []) -> String { 134 let regex = try! NSRegularExpression(pattern: pattern, options: options) 135 return regex.stringByReplacingMatches(in: self, 136 options: [], 137 range:NSMakeRange(0, self.count), 138 withTemplate: with) 139 } 140 }

[Swift]擴展String類:extension String