The NSMutable​String 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 NSMutable​String class adds one primitive method—replace​Characters(in:​with:​)—to the basic string-handling behavior inherited from NSString. All other methods that modify a string work through this method. For example, insert(_:​at:​) simply replaces the characters in a range of 0 length, while delete​Characters(in:​) replaces the characters in a given range with no characters.

NSMutableString is “toll-free bridged” with its Core Foundation counterpart, CFMutable​String. See Toll-Free Bridging for more information.


Creating and Initializing a Mutable String

init(capacity:​ Int)

Returns an NSMutable​String object initialized with initial storage for a given number of characters,

Modifying a String

func append(String)

Adds to the end of the receiver the characters of a given string.

func apply​Transform(String, reverse:​ Bool, range:​ NSRange, updated​Range:​ NSRange​Pointer?)

Transliterates the receiver by applying a specified ICU string transform.

func delete​Characters(in:​ NSRange)

Removes from the receiver the characters in a given range.

func insert(String, at:​ Int)

Inserts into the receiver the characters of a given string at a given location.

func replace​Characters(in:​ NSRange, with:​ String)

Replaces the characters from a​Range with those in a​String.

func replace​Occurrences(of:​ String, with:​ String, options:​ NSString.Compare​Options = [], range:​ NSRange)

Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.

func set​String(String)

Replaces the characters of the receiver with those in a given string.