3 回答
TA贡献1836条经验 获得超13个赞
注意:
SWIFT 4
Substring
试试看 这里
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)] }}
为什么这不是内置的?
带有整数的子脚本字符串不可用。
“ 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()
等。
- 3 回答
- 0 关注
- 2143 浏览
添加回答
举报