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 關注
- 2189 瀏覽
添加回答
舉報