Friday, May 6, 2011

NSString stringByAppendingString example objc

Some NSString stringByAppendingString methods are listed below. One is creating new NSString object from two NSString objects and the other is using NSMutableString class.

1. NSString concatenation by creating a new NSString object from two NSString objects.

NSString *string1 = @"This is";
NSString *string2 = @" a test.";
NSString *string3 = [string1 stringByAppendingString:string2];
 or

NSString *string1 = @"This is";
NSString *string2 = @" a test.";
string1 = [string1 stringByAppendingString:string2];

2. NSString concatenation using NSMutableString object.
NSMutableString *string1 = @"This is";
NSString *string2 = @" a test.";
[string1 appendString:string2];