NSMutableString Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in iOS 2.0 and later. |
| Companion guide | |
| Declared in | NSString.h |
Overview
The NSMutableString class declares the programmatic interface to an object that manages a mutable string—that is, a string whose contents can be edited—that conceptually represents an array of Unicode characters. To construct and manage an immutable string—or a string that cannot be changed after it has been created—use an object of the NSString class.
The NSMutableString class adds one primitive method—replaceCharactersInRange:withString:—to the basic string-handling behavior inherited from NSString. All other methods that modify a string work through this method. For example, insertString:atIndex: simply replaces the characters in a range of 0 length, while deleteCharactersInRange: replaces the characters in a given range with no characters.
NSMutableString is “toll-free bridged” with its Core Foundation counterpart, CFMutableStringRef. See “Toll-Free Bridging” for more information.
Class Methods
stringWithCapacity:
Returns an empty NSMutableString object with initial storage for a given number of characters.
Parameters
- capacity
The number of characters the string is expected to initially contain.
Return Value
An empty NSMutableString object with initial storage for capacity characters.
Discussion
The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hInstance Methods
appendFormat:
Adds a constructed string to the receiver.
Parameters
- format
A format string. See “Formatting String Objects” for more information. This value must not be
nil.Important: Raises an
NSInvalidArgumentExceptionif format isnil.- ...
A comma-separated list of arguments to substitute into format.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSString.happendString:
Adds to the end of the receiver the characters of a given string.
Parameters
- aString
The string to append to the receiver. aString must not be
nil
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSString.hdeleteCharactersInRange:
Removes from the receiver the characters in a given range.
Parameters
- aRange
The range of characters to delete. aRange must not exceed the bounds of the receiver.
Important: Raises an
NSRangeExceptionif any part of aRange lies beyond the end of the string.
Discussion
This method treats the length of the string as a valid range value that returns an empty string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hinitWithCapacity:
Returns an NSMutableString object initialized with initial storage for a given number of characters,
Parameters
- capacity
The number of characters the string is expected to initially contain.
Return Value
An initialized NSMutableString object with initial storage for capacity characters. The returned object might be different than the original receiver.
Discussion
The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hinsertString:atIndex:
Inserts into the receiver the characters of a given string at a given location.
Parameters
- aString
The string to insert into the receiver. aString must not be
nil.- anIndex
The location at which aString is inserted. The location must not exceed the bounds of the receiver.
Important: Raises an
NSRangeExceptionif anIndex lies beyond the end of the string.
Discussion
The new characters begin at anIndex and the existing characters from anIndex to the end are shifted by the length of aString.
This method treats the length of the string as a valid index value that returns an empty string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hreplaceCharactersInRange:withString:
Replaces the characters from aRange with those in aString.
Parameters
- aRange
The range of characters to replace. aRange must not exceed the bounds of the receiver.
Important: Raises an
NSRangeExceptionif any part of aRange lies beyond the end of the receiver.- aString
The string with which to replace the characters in
aRange. aString must not benil.
Discussion
This method treats the length of the string as a valid range value that returns an empty string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hreplaceOccurrencesOfString:withString:options:range:
Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.
Parameters
- target
The string to replace.
Important: Raises an
NSInvalidArgumentExceptionif target isnil.- replacement
The string with which to replace target.
Important: Raises an
NSInvalidArgumentExceptionif replacement isnil.- opts
A mask specifying search options. See String Programming Guide for details.
If opts is
NSBackwardsSearch, the search is done from the end of the range. If opts isNSAnchoredSearch, only anchored (but potentially multiple) instances are replaced.NSLiteralSearchandNSCaseInsensitiveSearchalso apply.- searchRange
The range of characters to replace. aRange must not exceed the bounds of the receiver. Specify searchRange as
NSMakeRange(0, [receiver length])to process the entire string.Important: Raises an
NSRangeExceptionif any part of searchRange lies beyond the end of the receiver.
Return Value
The number of replacements made.
Discussion
This method treats the length of the string as a valid range value that returns an empty string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.hsetString:
Replaces the characters of the receiver with those in a given string.
Parameters
- aString
The string with which to replace the receiver's content. aString must not be
nil.
Availability
- Available in iOS 2.0 and later.
Declared In
NSString.h© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-03-06)