我發現String.Index在 Swift 中使用確實需要很多代碼,尤其是當涉及到RangeSwift 沒有的方法時。就像上面的代碼一樣,我不想要一個開放的范圍(兩個邊界都是唯一的)。所以我想知道我是否可以擴展String或Range簡化它。在下面的代碼中,我已經知道startIndexandendIndex是 type String.Index,并且startIndex..<endIndex是 type Range<String.Index>。但是當我擴展時String.Index,我想定義一個類似的方法static func >.< (lhs: String.Index, rhs: String.Index) -> Range<String.Index>,但是我失敗了,因為沒有方法可以String.Index提高或降低。
let startIndex = str.index(str.firstIndex(of: "[") ?? str.startIndex, offsetBy: 1)
let endIndex = str.firstIndex(of: "]") ?? str.startIndex
let subStr = str[startIndex..<endIndex]
我想定義如下運算子。用區間表示法來澄清它們,如果原生方法a...b等價于 [a, b] 并且a..<b等價于 [a, b),那么 (a, b) 和 (a,b] 等價于什么。
let startIndex = str.firstIndex(of: "[") ?? str.startIndex
let endIndex = str.firstIndex(of: "]") ?? str.startIndex
let subStr1 = str[startIndex...endIndex] // [a, b]
let subStr3 = str[startIndex..<endIndex] // [a, b)
let subStr2 = str[startIndex>.<endIndex] // (a, b)
let subStr4 = str[startIndex>..endIndex] // (a, b]
uj5u.com熱心網友回復:
編輯:請參閱第二個代碼示例!請忽略第一個塊;我沒有仔細閱讀這個問題,所以這不是一個相關的解決方案。
編輯2:另請查看此答案下方的第一條評論,以獲取第一個示例的主要警告示例。它不適用于包含表情符號的字串。感謝 Leo Dabus 的這一發現。
此代碼可能會滿足您的需求,但我不能保證它在所有情況下的可靠性。
它的作用是采用常規的半開整數范圍(形式a..<b)和特定字串(因為正如 matt 在他的評論中提到的那樣,Swift 字串索引與 class 無關String,僅與某些特定字串有關),并回傳該特定字串的開放索引范圍。
實際上,它只是將下限加 1 以將其從包含更改為排除。
import Foundation
// Returns a Range of 'String.Index'es where both upper and lower bounds are exclusive.
extension Range {
// Here, the upper bound of the range was already exclusive, so don't subtract 1 from it. Only add 1 to the lower bound.
static func OpenRange(string: String, range: Range<Int>) -> Range<String.Index> {
return Range<String.Index>(uncheckedBounds: (String.Index(utf16Offset: range.lowerBound 1, in: string), String.Index(utf16Offset: range.upperBound, in: string)))
}
}
let theString = "abcdefg"
let endIndex = theString.index(theString.startIndex, offsetBy: 5)
let range: Range<String.Index> = theString.startIndex..<endIndex
let openRange = Range<String.Index>.OpenRange(string: theString, range: 0..<5)
print(theString[range]) // prints 'abcde'
print(theString[openRange]) // prints 'bcde'
參考:https ://www.avanderlee.com/swift/ranges-explained/
不過,我上面的示例并不真正適合您所詢問的特定情況,因為我是從我已經擁有的代碼開始并試圖將其“適應”到這種情況。我的錯!我相信這是一個更好的選擇:
// Returns a Range of 'String.Index'es between two specified characters in the string (as exclusive bounds)
extension Range {
// Here, the upper bound of the range was already exclusive, so don't subtract 1 from it. Only add 1 to the lower bound.
static func OpenRange(string: String, from: String, to: String) -> Range<String.Index> {
// The 'firstIndex' method expects a Character, not a String, so you need a type cast here
let lowerInclusive = string.firstIndex(of: Character(from))!
let lowerExclusive = string.index(lowerInclusive, offsetBy: 1)
let upperExclusive = string.firstIndex(of: Character(to))!
return Range<String.Index>(uncheckedBounds: (lowerExclusive, upperExclusive))
}
}
let theString = "[10:02.11]"
let openRange1 = Range<String.Index>.OpenRange(string: theString, from: "[", to: ":")
let openRange2 = Range<String.Index>.OpenRange(string: theString, from: ":", to: "]")
print(theString[openRange1]) // prints '10'
print(theString[openRange2]) // prints '02.11'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/475459.html
