Objective-C NSMutableString – How to change a NSString
April 16, 2009 § 3 Comments
NSString is similar to Java String class. NSString is not immutable.
You can change them by taking off characters or adding new characters.
Objective-C offers a subclass of NSString, NSMutableString. Now you can slice or dice a string. NSMutableString is similar to Java’s StringBuffer class.
Create a new NSMutableString by using the class method stringWithCapacity:. NSMutableString preallocate a block of memory to hold it. It makes the operations much faster. appendString: method will add the string. appendFormat: method will append the formatted string to the end of receiving string.
string = [NSMutableString stringWithCapacity: 50];
[string appendString: @”Welcome “];
[string appendFormat: @”User ID:%d”, 123456];
String will display the value of “Welcome User ID:123456”.