<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFStringFindAndReplace(_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@F@CFStringFindAndReplace"
  },
  "title" : "CFStringFindAndReplace(_:_:_:_:_:)"
}
-->

# CFStringFindAndReplace(_:_:_:_:_:)

Replaces all occurrences of a substring within a given range.

```
func CFStringFindAndReplace(_ theString: CFMutableString!, _ stringToFind: CFString!, _ replacementString: CFString!, _ rangeToSearch: CFRange, _ compareOptions: CFStringCompareFlags) -> CFIndex
```

## Parameters

`theString`

The string to modify.

`stringToFind`

The substring to search for in `theString`.

`replacementString`

The replacement string for `stringToFind`.

`rangeToSearch`

The range within which to search in `theString`.

`compareOptions`

Flags that select different types of comparisons, such as localized comparison, case-insensitive comparison, and non-literal comparison. If you want the default comparison behavior, pass `0`. See [`CFStringCompareFlags`](/documentation/CoreFoundation/CFStringCompareFlags) for the available flags.

## Return Value

The number of instances of `stringToFind` that were replaced.

## Discussion

The possible values of `compareOptions` are combinations of the [`compareCaseInsensitive`](/documentation/CoreFoundation/CFStringCompareFlags/compareCaseInsensitive), [`compareBackwards`](/documentation/CoreFoundation/CFStringCompareFlags/compareBackwards), [`compareNonliteral`](/documentation/CoreFoundation/CFStringCompareFlags/compareNonliteral), and [`compareAnchored`](/documentation/CoreFoundation/CFStringCompareFlags/compareAnchored) constants.

The `kCFCompareBackwards` option can be used to replace a substring starting from the end, which could produce different results. For example, if the parameter `theString` is “AAAAA”, `stringToFind` is “AA”, and `replacementString` is “B”, then the result is normally “BBA”. However, if the `kCFCompareBackwards` constant is used, the result is “ABB.”

The `kCFCompareAnchored` option assures that only anchored but multiple instances are found (the instances must be consecutive at start or end). For example, if the parameter `theString` is “AAXAA”, `stringToFind` is “A”, and `replacementString` is “B”, then the result is normally “BBXBB.” However, if the `kCFCompareAnchored` constant is used, the result is “BBXAA.”

---

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)