Search code examples
objective-cnsstringstring-concatenation

Shortcuts in Objective-C to concatenate NSStrings


Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general?

For example, I'd like to make:

NSString *myString = @"This";
NSString *test = [myString stringByAppendingString:@" is just a test"];

something more like:

string myString = "This";
string test = myString + " is just a test";

Solution

  • Two answers I can think of... neither is particularly as pleasant as just having a concatenation operator.

    First, use an NSMutableString, which has an appendString method, removing some of the need for extra temp strings.

    Second, use an NSArray to concatenate via the componentsJoinedByString method.