Contents

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

Replaces all occurrences of a substring within a given range.

Declaration

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 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, 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