Skip to content

Commit ecf47df

Browse files
author
Eric Wu
committed
增加获取字符串位置接口
1 parent 23fe0ef commit ecf47df

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

CRBoostSwift/Classes/Foundation+CRBoost.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ extension String {
278278
}
279279
}
280280

281+
extension Collection where Element: Equatable {
282+
public func indexDistance(of element: Element) -> Int? {
283+
guard let index = firstIndex(of: element) else { return nil }
284+
return distance(from: startIndex, to: index)
285+
}
286+
}
287+
288+
extension StringProtocol {
289+
public func indexDistance(of string: Self) -> Int? {
290+
guard let index = range(of: string)?.lowerBound else { return nil }
291+
return distance(from: startIndex, to: index)
292+
}
293+
}
294+
281295
// https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
282296
extension String {
283297
public subscript(i: Int) -> Character {

0 commit comments

Comments
 (0)