请参考本博客另一篇技术博文:《[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 }
原文地址:https://www.cnblogs.com/strengthen/p/9882351.html
时间: 2024-10-12 21:00:02