<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/String/Index/samePosition(in:)-3mz95",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SS5IndexV12samePosition2inABSgSS8UTF8ViewV_tF"
  },
  "title" : "samePosition(in:)"
}
-->

# samePosition(in:)

Returns the position in the given UTF-8 view that corresponds exactly to
this index.

```
func samePosition(in utf8: String.UTF8View) -> String.UTF8View.Index?
```

## Parameters

`utf8`

The view to use for the index conversion. This index
must be a valid index of at least one view of the string shared by
`utf8`.

## Return Value

The position in `utf8` that corresponds exactly to this index.
If this index does not have an exact corresponding position in `utf8`,
this method returns `nil`. For example, an attempt to convert the
position of a UTF-16 trailing surrogate returns `nil`.

## Discussion

This example first finds the position of the character `"é"`, and then
uses this method find the same position in the string’s `utf8` view.

```
let cafe = "Café"
if let i = cafe.firstIndex(of: "é") {
    let j = i.samePosition(in: cafe.utf8)!
    print(Array(cafe.utf8[j...]))
}
// Prints "[195, 169]"
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)