一尘不染

在 Swift String 中查找字符索引

Swift

在 Objective-C 中,我可以使用类似的东西:

NSString* str = @"abcdefghi";
[str rangeOfString:@"c"].location; // 2

在 Swift 中,我看到类似的东西:

var str = "abcdefghi"
str.rangeOfString("c").startIndex

…但这只是给了我一个String.Index,我可以用它来下标回到原始字符串,但不能从中提取位置。

FWIW,它String.Index有一个名为的私有 ivar _position,其中包含正确的值。我只是不明白它是如何暴露的。

我知道我可以自己轻松地将它添加到 String 中。我更好奇我在这个新 API 中缺少什么。


阅读 149

收藏
2022-05-05

共1个答案

一尘不染

String没有实现RandomAccessIndexType。可能是因为它们启用了具有不同字节长度的字符。这就是为什么我们必须使用string.characters.countcountcountElements在 Swift 1.x 中)来获取字符数的原因。这也适用于职位。这_position可能是原始字节数组的索引,他们不想公开它。这String.Index旨在保护我们不访问字符中间的字节。

这意味着您获得的任何索引都必须从String.startIndexor String.endIndex( String.Indeximplements BidirectionalIndexType) 创建。可以使用successororpredecessor方法创建任何其他索引。

现在为了帮助我们处理索引,有一组方法(Swift 1.x 中的函数):

Swift4.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)

Swift3.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很麻烦,但使用包装器按整数索引是危险的,因为它隐藏了实际索引的低效率。

请注意,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]  
2022-05-05