在SWIFT字符串中查找字符索引 [英] Finding index of character in Swift String
问题描述
是时候承认失败了…
在Objective-C中,我可以使用如下内容:
NSString* str = @"abcdefghi";
[str rangeOfString:@"c"].location; // 2
在SWIFT中,我看到了类似的情况:
var str = "abcdefghi"
str.rangeOfString("c").startIndex
...但这只是给了我一个String.Index
,我可以用它来向回放回原始字符串的下标,但不能从中提取位置。
String.Index
有一个名为_position
私有IVAR,其中包含正确的值。我就是不明白它是怎么曝光的。
我知道我可以很容易地将它添加到字符串中。我更好奇的是我在这个新的API中遗漏了什么。
推荐答案
您不是唯一找不到解决方案的人。
String
不实现RandomAccessIndexType
。可能是因为它们支持具有不同字节长度的字符。这就是我们必须使用string.characters.count
(SWIFT 1.x中的count
或countElements
)来获取字符数的原因。这也适用于职位。_position
可能是原始字节数组的索引,他们不想公开这一点。String.Index
旨在防止我们访问字符中间的字节。
这意味着您获得的任何索引都必须从String.startIndex
或String.endIndex
(String.Index
实现BidirectionalIndexType
)创建。可以使用successor
或predecessor
方法创建任何其他索引。
现在为了帮助我们处理索引,有一套方法(SWIFT 1.x中的函数):
SWIFT 4.x
let text = "abc"
let index2 = text.index(text.startIndex, offsetBy: 2) //will call succ 2 times
let lastChar: Character = text[index2] //now we can index!
let characterIndex2 = text.index(text.startIndex, offsetBy: 2)
let lastChar2 = text[characterIndex2] //will do the same as above
let range: Range<String.Index> = text.range(of: "b")!
let index: Int = text.distance(from: text.startIndex, to: range.lowerBound)
SWIFT 3.0
let text = "abc"
let index2 = text.index(text.startIndex, offsetBy: 2) //will call succ 2 times
let lastChar: Character = text[index2] //now we can index!
let characterIndex2 = text.characters.index(text.characters.startIndex, offsetBy: 2)
let lastChar2 = text.characters[characterIndex2] //will do the same as above
let range: Range<String.Index> = text.range(of: "b")!
let index: Int = text.distance(from: text.startIndex, to: range.lowerBound)
SWIFT 2.x
let text = "abc"
let index2 = text.startIndex.advancedBy(2) //will call succ 2 times
let lastChar: Character = text[index2] //now we can index!
let lastChar2 = text.characters[index2] //will do the same as above
let range: Range<String.Index> = text.rangeOfString("b")!
let index: Int = text.startIndex.distanceTo(range.startIndex) //will call successor/predecessor several times until the indices match
SWIFT 1.x
let text = "abc"
let index2 = advance(text.startIndex, 2) //will call succ 2 times
let lastChar: Character = text[index2] //now we can index!
let range = text.rangeOfString("b")
let index: Int = distance(text.startIndex, range.startIndex) //will call succ/pred several times
使用String.Index
很麻烦,但使用包装按整数编制索引(请参见https://stackoverflow.com/a/25152652/669586)是危险的,因为它隐藏了实际索引的低效。
请注意,SWIFT索引实现存在为一个字符串创建的索引/范围无法可靠地用于另一个字符串的问题,例如:
SWIFT 2.x
let text: String = "abc"
let text2: String = "🎾🏇🏈"
let range = text.rangeOfString("b")!
//can randomly return a bad substring or throw an exception
let substring: String = text2[range]
//the correct solution
let intIndex: Int = text.startIndex.distanceTo(range.startIndex)
let startIndex2 = text2.startIndex.advancedBy(intIndex)
let range2 = startIndex2...startIndex2
let substring: String = text2[range2]
SWIFT 1.x
let text: String = "abc"
let text2: String = "🎾🏇🏈"
let range = text.rangeOfString("b")
//can randomly return nil or a bad substring
let substring: String = text2[range]
//the correct solution
let intIndex: Int = distance(text.startIndex, range.startIndex)
let startIndex2 = advance(text2.startIndex, intIndex)
let range2 = startIndex2...startIndex2
let substring: String = text2[range2]
这篇关于在SWIFT字符串中查找字符索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!