Mutable Strings

You can use the NSMutableString class to create string objects whose characters can be changed. Because this class is a subclass of NSString, you can use all of NSString’s methods as well.

When we speak of mutable versus immutable string objects, we are talking about changing the actual characters within the string. Either a mutable or an immutable string object can always be set to a completely different string object during execution of the program. This was stressed in the discussion of Program 15.4. So, for example, consider the following:

str1 = @"This is a string";   ...str1 = [str1 substringFromIndex: 5];

In this case, str1 is first set to a constant character string object. Later in the program, ...

Get Programming in Objective-C, Sixth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.