File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed
Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff 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
282296extension String {
283297 public subscript( i: Int ) -> Character {
You can’t perform that action at this time.
0 commit comments