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.

NSMutableString *string;
string = [NSMutableString stringWithCapacity: 50];
[string appendString: @”Welcome “];
[string appendFormat: @”User ID:%d”, 123456];

String will display the value of “Welcome User ID:123456”.

Advertisements

§ 3 Responses to Objective-C NSMutableString – How to change a NSString

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Objective-C NSMutableString – How to change a NSString at Web Builders.

meta

%d bloggers like this: