---
title: "CFStringFindAndReplace(_:_:_:_:_:)"
framework: corefoundation
role: symbol
role_heading: Function
path: "corefoundation/cfstringfindandreplace(_:_:_:_:_:)"
---

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

Replaces all occurrences of a substring within a given range.

## Declaration

```swift
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 doc://com.apple.corefoundation/documentation/CoreFoundation/CFStringCompareFlags for the available flags.

## Return Value

Return Value The number of instances of stringToFind that were replaced.

## Discussion

Discussion The possible values of compareOptions are combinations of the compareCaseInsensitive, compareBackwards, compareNonliteral, and 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.”

## See Also

### CFMutableString Miscellaneous Functions

- [CFStringAppend(_:_:)](corefoundation/cfstringappend(_:_:).md)
- [CFStringAppendCharacters(_:_:_:)](corefoundation/cfstringappendcharacters(_:_:_:).md)
- [CFStringAppendCString(_:_:_:)](corefoundation/cfstringappendcstring(_:_:_:).md)
- [CFStringAppendFormatAndArguments(_:_:_:_:)](corefoundation/cfstringappendformatandarguments(_:_:_:_:).md)
- [CFStringAppendPascalString(_:_:_:)](corefoundation/cfstringappendpascalstring(_:_:_:).md)
- [CFStringCapitalize(_:_:)](corefoundation/cfstringcapitalize(_:_:).md)
- [CFStringCreateMutable(_:_:)](corefoundation/cfstringcreatemutable(_:_:).md)
- [CFStringCreateMutableCopy(_:_:_:)](corefoundation/cfstringcreatemutablecopy(_:_:_:).md)
- [CFStringCreateMutableWithExternalCharactersNoCopy(_:_:_:_:_:)](corefoundation/cfstringcreatemutablewithexternalcharactersnocopy(_:_:_:_:_:).md)
- [CFStringDelete(_:_:)](corefoundation/cfstringdelete(_:_:).md)
- [CFStringFold(_:_:_:)](corefoundation/cfstringfold(_:_:_:).md)
- [CFStringInsert(_:_:_:)](corefoundation/cfstringinsert(_:_:_:).md)
- [CFStringLowercase(_:_:)](corefoundation/cfstringlowercase(_:_:).md)
- [CFStringNormalize(_:_:)](corefoundation/cfstringnormalize(_:_:).md)
- [CFStringPad(_:_:_:_:)](corefoundation/cfstringpad(_:_:_:_:).md)
