开心每一天1111
注意:请看利奥·达布斯的回答对于SWIFT 4的适当实现。SWIFT 4这个Substring在SWIFT 4中引入了类型,以便通过与原始字符串共享存储,使子字符串更快、更高效,因此下标函数应该返回该类型。试试看这里extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
subscript (bounds: CountableRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ..< end]
}
subscript (bounds: CountableClosedRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ... end]
}
subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(endIndex, offsetBy: -1)
return self[start ... end]
}
subscript (bounds: PartialRangeThrough<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ... end]
}
subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ..< end]
}}extension Substring {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
subscript (bounds: CountableRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ..< end]
}
subscript (bounds: CountableClosedRange<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[start ... end]
}
subscript (bounds: CountablePartialRangeFrom<Int>) -> Substring {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(endIndex, offsetBy: -1)
return self[start ... end]
}
subscript (bounds: PartialRangeThrough<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ... end]
}
subscript (bounds: PartialRangeUpTo<Int>) -> Substring {
let end = index(startIndex, offsetBy: bounds.upperBound)
return self[startIndex ..< end]
}}若要转换Substring变成String,你可以简单地做String(string[0..2]),但只有当您计划保留子字符串时,才应该这样做。否则,将其保持为Substring.如果有人能想出一个很好的方法把这两个扩展合并成一个,那就太好了。我试着扩展StringProtocol没有成功,因为index方法不存在。 SWIFT 3:extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
subscript (i: Int) -> String {
return String(self[i] as Character)
}
subscript (r: Range<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
let end = index(startIndex, offsetBy: r.upperBound)
return self[Range(start ..< end)]
}} 为什么这不是内置的?Apple提供了以下解释(在这里发现的):带有整数的子脚本字符串不可用。“i字符串中的TH字符在不同的库和系统组件中有不同的解释。正确的解释应该根据用例和所涉及的api选择,所以String不能用整数订阅。SWIFT提供了几种访问字符串中存储的字符数据的不同方法。String.utf8是字符串中UTF-8代码单元的集合。将字符串转换为UTF-8时使用此API。大多数POSIX API以UTF-8代码单元处理字符串。String.utf16是字符串中UTF-16代码单元的集合。大多数Cocoa和CocoaTouch API根据UTF-16代码单元处理字符串。例如,NSRange与.连用NSAttributedString和NSRegularExpression以UTF-16码单元存储子字符串偏移量和长度。String.unicodeScalars是Unicode标量的集合。当您执行字符数据的低级别操作时,请使用此API。String.characters是一组扩展的字素聚类,它是用户感知字符的近似。请注意,在处理包含可读的文本的字符串时,应尽可能避免逐字符处理。例如,使用高级别的对地区敏感的Unicode算法,String.localizedStandardCompare(),String.localizedLowercaseString, String.localizedStandardRangeOfString()等。