You can choose from a variety of String Services functions to add to and modify the contents of mutable CFString objects. These functions, as one might expect, do not work on immutable CFString objects. If you want to change the contents of a CFString object, you must either start with a content-less mutable CFString object or make a mutable copy of an immutable CFString object. See Creating Mutable String Objects for information on creating objects of this kind.
The functions that manipulate mutable CFString objects fall into several categories:
CFStringAppend
), C and Pascal strings (
CFStringAppendCString
and
CFStringAppendPascalString
), Unicode characters (
CFStringAppendCharacters
), and formatted strings (
CFStringAppendFormat
and
CFStringAppendFormatAndArguments
).
CFStringInsert
,
CFStringDelete
, and
CFStringReplace
peform the corresponding operations. These functions require you to specify a zero-based index into, or range of, the string to be modified.
CFStringPad
function extends or truncates a mutable CFString to a given length; if it extends the string, it pads with a specified character or characters. The
CFStringTrim
function trims a specific character from both sides of the string. For example, the call:CFStringTrim(CFSTR("xxxabcx"), CFSTR("x"));
would result in the string "abc". A related function,
CFStringTrimWhitespace
, does the same thing with whitespace characters, which include such characters as tabs and carriage returns.
CFStringUppercase
), all lowercase (
CFStringLowercase
), or just the first character of the string uppercase (
CFStringCapitalize
).Listing 12 exemplifies several of the functions that manipulate mutable CFString objects:
Listing 12 Various operations on a mutable string
void mutableStringOperations() { CFMutableStringRef mstr; CFRange range; StringPtr pbuf; CFIndex length; mstr = CFStringCreateMutable(NULL, 0); CFStringAppend(mstr, CFSTR("Now is the time for all good men to come to the aid of their ")); CFStringAppend(mstr, CFSTR("party.")); CFShow(CFSTR("Mutable String 1 - Appended CFStrings")); CFShow(mstr); range = CFStringFind(mstr, CFSTR("good"), 0); if (range.length > 0) { CFStringReplace(mstr, range, CFSTR("bad")); CFShow(CFSTR("Mutable String 2 - Replaced substring")); CFShow(mstr); } CFStringAppendPascalString(mstr, "\p Now is the time for a party.", CFStringGetSystemEncoding()); CFStringDelete(mstr, CFRangeMake(10, 20)); CFShow(CFSTR("Mutable String 3 - Pascal string added, characters in middle deleted:")); CFShow(mstr); CFStringUppercase(mstr, NULL); CFShow(CFSTR("Mutable String 4 - Convert to uppercase:")); CFShow(mstr); }
When compiled and run, this code generates the following output:
Mutable String 1 - Appended CFStrings Now is the time for all good men to come to the aid of their party. Mutable String 2 - Replaced substring Now is the time for all bad men to come to the aid of their party. Mutable String 3 - Pascal string added, characters in middle deleted: Now is then to come to the aid of their party. Now is the time for a party. Mutable String 4 - Convert to uppercase: NOW IS THEN TO COME TO THE AID OF THEIR PARTY. NOW IS THE TIME FOR A PARTY.