<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSRegularExpression/replacementString(for:in:offset:template:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSRegularExpression(im)replacementStringForResult:inString:offset:template:"
  },
  "title" : "replacementString(for:in:offset:template:)"
}
-->

# replacementString(for:in:offset:template:)

Used to perform template substitution for a single result for clients implementing their own replace functionality.

```
func replacementString(for result: NSTextCheckingResult, in string: String, offset: Int, template templ: String) -> String
```

## Parameters

`result`

The result of the single match.

`string`

The string from which the result was matched.

`offset`

The offset to be added to the location of the result in the string.

`templ`

See [`Flag Options`](/documentation/Foundation/NSRegularExpression#Flag-Options) for the format of `template`.

## Return Value

A replacement string.

## Discussion

For clients implementing their own replace functionality, this is a method to perform the template substitution for a single result, given the string from which the result was matched, an offset to be added to the location of the result in the string (for example, in cases that modifications to the string moved the result since it was matched), and a replacement template.

This is an advanced method that is used only if you wanted to iterate through a list of matches yourself and do the template replacement for each one, plus maybe some other calculation that you want to do in code, then you would use this at each step.

---

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)