<!--
{
  "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:)-6oxfv",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SS5IndexV12samePosition2inABSgSS_tF"
  },
  "title" : "samePosition(in:)"
}
-->

# samePosition(in:)

Returns the position in the given string that corresponds exactly to this
index.

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

## Parameters

`characters`

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

## Return Value

The position in `characters` that corresponds exactly to
this index. If this index does not have an exact corresponding
position in `characters`, this method returns `nil`. For example,
an attempt to convert the position of a UTF-8 continuation byte
returns `nil`.

## Discussion

This example first finds the position of a space (UTF-8 code point `32`)
in a string’s `utf8` view and then uses this method find the same position
in the string.

```
let cafe = "Café 🍵"
let i = cafe.unicodeScalars.firstIndex(of: "🍵")!
let j = i.samePosition(in: cafe)!
print(cafe[j...])
// Prints "🍵"
```

---

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)