如何定义自定义运算符以处理swift中的string.index范围?
我发现使用string.index
确实需要大量Swift中的代码,尤其是当涉及range> range
Swift没有的方法时。就像上面的代码中,我不想要开放范围(两者都独家)。因此,我想知道我是否可以扩展字符串
或range
或简化它的内容。在以下代码中,我已经知道startIndex
和endIndex
是类型string.index.index
和startIndex ..< endIndIndex
是类型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]
我想在下面定义运营商。使用间隔表示法,如果本机方法b
等同于[a,b]和a ..&lt; 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]
I find using String.Index
really needs a lot of code in Swift, especially when it comes to methods of Range
Swift doesn't have. Like in above code where I don't want an open range (both bounds exclusive). So I wonder if I can extend String
or Range
or something to simplify it. In the following code, I already know that startIndex
and endIndex
are of type String.Index
, and startIndex..<endIndex
is of type Range<String.Index>
. But when I extend String.Index
, I'd like to define a method like static func >.< (lhs: String.Index, rhs: String.Index) -> Range<String.Index>
, but I failed because there's no method to step String.Index
up or down.
let startIndex = str.index(str.firstIndex(of: "[") ?? str.startIndex, offsetBy: 1)
let endIndex = str.firstIndex(of: "]") ?? str.startIndex
let subStr = str[startIndex..<endIndex]
I want to define operators like below. To clarify them using interval notation, if native method a...b
is equivalent to [a, b] and a..<b
is equivalent to [a, b), what is the equivalent of (a, b) and (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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:而是请参见第二代码示例!请忽略第一个街区;我没有仔细阅读这个问题,因此这不是一个相关的解决方案。
编辑2:还请查看下面的第一个评论,以获取第一个示例的主要警告的示例。它对于包含表情符号的字符串无法正常工作。归功于Leo Dabus的这一发现。
该代码可能会执行您要寻找的内容,但在所有情况下我都不能保证其可靠性。
它的作用是进行常规的半开整个整数范围(表单
a ..&lt; b
)和一个特定的字符串(因为,正如Matt在他的评论中提到的那样,Swift String Indices Don' t涉及类字符串
,仅与某些特定的字符串),并返回该特定字符串的开放范围。实际上,它只是将其从包容性更改为独家的下限中增加了1个。
参考:
我上面的示例并不真正符合您要问的特定情况,因为我是从已经开始的代码开始的,并且试图“适应”这种情况。我的坏!我相信这是一个更好的选择:
Edit: See the second code example instead! Please disregard the first block; I didn't read the question as carefully as I should have, so it is not a relevant solution.
Edit 2: Also look at the first comment below this answer for an example of a major caveat of the first example. It does not work correctly for strings which contain emojis. Credit to Leo Dabus for this discovery.
This code might do what you're looking for, but I can't guarantee its reliability in all cases.
What it does is take a regular half-open range of integers (of the form
a..<b
) and a specific string (because, as matt mentioned in his comment, Swift string indices don't pertain to the classString
, only to some particular string), and returns an open range of indices for that particular string.In effect, it just adds 1 to the lower bound to change it from inclusive to exclusive.
Reference: https://www.avanderlee.com/swift/ranges-explained/
The example I had above doesn't really fit the specific case you were asking about though, as I was starting from code I already had and was trying to 'adapt' it to this situation. My bad! I believe this is a better alternative: