<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.1.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UISearchTextField/replaceTextualPortion(of:with:at:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UISearchTextField(im)replaceTextualPortionOfRange:withToken:atIndex:"
  },
  "title" : "replaceTextualPortion(of:with:at:)"
}
-->

# replaceTextualPortion(of:with:at:)

Converts text in a search field into a search token.

```
func replaceTextualPortion(of textRange: UITextRange, with token: UISearchToken, at tokenIndex: Int)
```

## Parameters

`textRange`

The text to remove.

`token`

The token to add.

`tokenIndex`

The location for the added token.

## Discussion

This method removes any text in the specified range, inserts the provided token at the specified index, and selects the newly inserted token. Prefer using this convenience method over performing each step with other methods. When your app calls [`replaceTextualPortion(of:with:at:)`](/documentation/UIKit/UISearchTextField/replaceTextualPortion(of:with:at:)), UIKit commits any marked text before modifying the text, and creates a single undo group.

This method doesn’t remove any tokens in the `textRange`, so you don’t have to manually trim the [`selectedTextRange`](/documentation/UIKit/UITextInput/selectedTextRange) before you use it in this method.

---

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)