---
title: "distance(from:to:)"
framework: swift
role: symbol
role_heading: Instance Method
path: "swift/string/utf8view/distance(from:to:)"
---

# distance(from:to:)

Returns the distance between two indices.

## Declaration

```swift
func distance(from i: String.UTF8View.Index, to j: String.UTF8View.Index) -> Int
```

## Return Value

Return Value The distance between start and end. The result can be negative only if the collection conforms to the BidirectionalCollection protocol.

## Discussion

Discussion Unless the collection conforms to the BidirectionalCollection protocol, start must be less than or equal to end. note: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(k), where k is the resulting distance.
