在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 请参考本博客另一篇技术博文: 《[Swift]字符串(String类、NSString类)常用操作》 1 extension String { 2 //获取字符串首字符 3 var first: String 4 { 5 get{return String(self[startIndex])} 6 } 7 8 // 获取字符串尾字符 9 var last: String 10 { 11 //注意最后一个字符的索引位置 12 get{return String(self[index(before: self.endIndex)])} 13 } 14 15 //字符串所表示数字的ASCII整数值(定义大写为字符值) 16 var ASCII:Character 17 { 18 get {return Character(UnicodeScalar(Int(self)!)!)} 19 } 20 21 //字符的ASCII整数值(定义小写为整数值) 22 var ascii: Int { 23 get{ 24 return Int(self.unicodeScalars.first!.value) 25 } 26 } 27 28 //获取指定索引位置的字符,返回为字符串形式 29 func charAt(_ num:Int) -> String 30 { 31 return String(self[index(self.startIndex,offsetBy: num)]) 32 } 33 34 // 将字符串转为指定进制的Int 35 //Radix is in range 2...36 36 func convertToInt(_ num:Int) -> Int 37 { 38 //Radix is in range 2...36 39 //判断区间是否包含包含某个特定的值 40 if (2...36).contains(num) 41 { 42 return Int(self, radix: num)! 43 } 44 return 0 45 } 46 47 //获取重复指定次数的字符串 48 func repeatString(_ times: Int ) -> String 49 { 50 var result = String() 51 for i in 0...times { 52 result += self 53 } 54 return result 55 } 56 57 //整体反转字符串 58 func reverse() -> String 59 { 60 return String(self.reversed()) 61 } 62 63 // 截取字符串:从起始处到index 64 // - Parameter index: 结束索引 65 // - Returns: 子字符串 66 func subStringTo(_ index: Int) -> String { 67 let theIndex = self.index(self.startIndex,offsetBy:min(self.count,index)) 68 return String(self[startIndex...theIndex]) 69 } 70 71 // 截取字符串:从index到结束处 72 // - Parameter index: 开始索引 73 // - Returns: 子字符串 74 func subStringFrom(_ index: Int) -> String { 75 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 76 return String(self[theIndex..<endIndex]) 77 } 78 79 // 截取字符串:指定区间 80 // - Parameter range: 闭区间 81 // - Returns: 子字符串 82 func subStringInRange(_ range: CountableClosedRange<Int>) -> String { 83 let start = self.index(self.startIndex, offsetBy: max(0, range.lowerBound)) 84 let end = self.index(self.startIndex, offsetBy: min(self.count, range.upperBound)) 85 return String(self[start...end]) 86 } 87 88 // 截取字符串:指定索引和字符数 89 // - begin: 开始截取处索引 90 // - count: 截取的字符数量 91 func subString(_ begin:Int,_ count:Int) -> String { 92 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 93 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 94 return String(self[start..<end]) 95 } 96 97 //subscript函数可以检索数组中的值 98 //直接按照索引方式截取指定索引的字符 99 subscript (_ i: Int) -> Character { 100 //读取字符 101 get {return self[index(startIndex, offsetBy: i)]} 102 103 //修改字符 104 set 105 { 106 //转换为字符数组 107 var arr:[Character] = Array(self) 108 arr[i] = newValue 109 self = String(arr) 110 } 111 } 112 113 //subscript函数可以检索数组中的值 114 //直接按照索引方式截取指定索引的字符串 115 subscript(_ numbers: Int...) -> String { 116 var str:String = String() 117 for num in numbers { 118 str.append(self[num]) 119 } 120 return str 121 } 122 123 //subscript函数可以检索数组中的值 124 //直接按照索引方式截取指定索引的字符串 125 subscript (bounds: CountableRange<Int>) -> Substring { 126 let start = index(startIndex, offsetBy: bounds.lowerBound) 127 let end = index(startIndex, offsetBy: bounds.upperBound) 128 return self[start ..< end] 129 } 130 131 subscript (bounds: CountableClosedRange<Int>) -> Substring { 132 let start = index(startIndex, offsetBy: bounds.lowerBound) 133 let end = index(startIndex, offsetBy: bounds.upperBound) 134 return self[start ... end] 135 } 136 137 subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring { 138 let start = index(startIndex, offsetBy: bounds.lowerBound) 139 let end = index(endIndex, offsetBy: -1) 140 return self[start ... end] 141 } 142 143 subscript (bounds: PartialRangeThrough<Int>) -> Substring { 144 let end = index(startIndex, offsetBy: bounds.upperBound) 145 return self[startIndex ... end] 146 } 147 148 subscript (bounds: PartialRangeUpTo<Int>) -> Substring { 149 let end = index(startIndex, offsetBy: bounds.upperBound) 150 return self[startIndex ..< end] 151 } 152 } 示例代码: 1 var str = "stringthen" 2 print(str.first) 3 // s 4 print(str.last) 5 // n 6 print("116".ASCII) 7 // t 注:ASCII中t为116 8 print("t".ascii) 9 // 116 注:ASCII中t为116 10 print(str.charAt(3)) 11 // i 12 print("F".convertToInt(16)) 13 // 15 14 print(str.repeatString(3)) 15 // stringthenstringthenstringthenstringthen 16 print(str[3]) 17 // i (获取第三个字符) 18 str = "123456789" 19 str[3] = "0" 20 print(str) 21 // str0ngthen (修改索引位置的字符) 22 print(str[3,6]) 23 // it (获取第3个和第6个字符,两个字符组成的字符串) 24 print(str[3..<6]) 25 // ing (获取连续的第3个到第5个字符,三个字符组成的字符串) 26 print(str[3...6]) 27 // ingt (获取连续的第3个到第6个字符,四个字符组成的字符串) 28 print(str.subStringTo(3)) 29 // stri 30 print(str.subStringFrom(3)) 31 // ingthen 32 print(str.subStringInRange(3...6)) 33 // ingt 34 print(str.subString(3,4)) 35 // ingt 36 //stride循环中用索引 37 for index in stride(from:str.count - 2, through: 0, by: -1) 38 { 39 print(str[index]) 40 print(str[0...index]) 41 print(str[0..<index]) 42 } 43 /* 44 s 45 n 46 t 47 116 48 i 49 15 50 stringthenstringthenstringthenstringthen 51 i 52 123056789 53 07 54 056 55 0567 56 1230 57 056789 58 0567 59 0567 60 8 61 12305678 62 1230567 63 7 64 1230567 65 123056 66 6 67 123056 68 12305 69 5 70 12305 71 1230 72 0 73 1230 74 123 75 3 76 123 77 12 78 2 79 12 80 1 81 1 82 1 83 */
|
请发表评论