Function

CFStringFindAndReplace

Replaces all occurrences of a substring within a given range.

Declaration

CFIndex CFStringFindAndReplace(CFMutableStringRef theString, CFStringRef stringToFind, CFStringRef replacementString, CFRange rangeToSearch, CFStringCompareFlags compareOptions);

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 kCFCompareCaseInsensitive, kCFCompareBackwards, kCFCompareNonliteral, and kCFCompareAnchored 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

Appends the characters of a string to those of a CFMutableString object.

CFStringAppendCharacters

Appends a buffer of Unicode characters to the character contents of a CFMutableString object.

CFStringAppendCString

Appends a C string to the character contents of a CFMutableString object.

CFStringAppendFormat

Appends a formatted string to the character contents of a CFMutableString object.

CFStringAppendFormatAndArguments

Appends a formatted string to the character contents of a CFMutableString object.

CFStringAppendPascalString

Appends a Pascal string to the character contents of a CFMutableString object.

CFStringCapitalize

Changes the first character in each word of a string to uppercase (if it is a lowercase alphabetical character).

CFStringCreateMutable

Creates an empty CFMutableString object.

CFStringCreateMutableCopy

Creates a mutable copy of a string.

CFStringCreateMutableWithExternalCharactersNoCopy

Creates a CFMutableString object whose Unicode character buffer is controlled externally.

CFStringDelete

Deletes a range of characters in a string.

CFStringFold

Folds a given string into the form specified by optional flags.

CFStringInsert

Inserts a string at a specified location in the character buffer of a CFMutableString object.

CFStringLowercase

Changes all uppercase alphabetical characters in a CFMutableString to lowercase.

CFStringNormalize

Normalizes the string into the specified form as described in Unicode Technical Report #15.

CFStringPad

Enlarges a string, padding it with specified characters, or truncates the string.

CFStringReplace

Replaces part of the character contents of a CFMutableString object with another string.

CFStringReplaceAll

Replaces all characters of a CFMutableString object with other characters.

CFStringSetExternalCharactersNoCopy

Notifies a CFMutableString object that its external backing store of Unicode characters has changed.

CFStringTransform

Perform in-place transliteration on a mutable string.

CFStringTrim

Trims a specified substring from the beginning and end of a CFMutableString object.

CFStringTrimWhitespace

Trims whitespace from the beginning and end of a CFMutableString object.

CFStringUppercase

Changes all lowercase alphabetical characters in a CFMutableString object to uppercase.