How do I concatenate to NSStrings
together in Objective C?
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.
A static, plain-text Unicode string object which you use when you need reference semantics or other Foundation-specific behavior.
If the string is not mutable, you will instead want:
NSString *firstString = @"FirstString";
NSString *secondString = @"SecondString";
NSString *concatinatedString = [firstString stringByAppendingString:secondString];
// Note that concatinatedString is autoreleased,
// so if you may want to [concaticatedString retain] it.
For completeness, here's the answer for a mutable string:
NSMutableString *firstString = [NSMutableString stringWithString:@"FirstString"];
NSString *secondString = @"SecondString";
[firstString appendString:secondString];
// Note that firstString is autoreleased,
// so if you may want to [firstString retain] it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With