8

How do I concatenate to NSStrings together in Objective C?

2
  • Are you asking how to concatenate two strings? Commented Feb 10, 2010 at 2:03
  • 2
    Duplicate of stackoverflow.com/questions/589395/… Commented Feb 10, 2010 at 2:06

2 Answers 2

22

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.
Sign up to request clarification or add additional context in comments.

Comments

9

If you have a mutable string then you can do:

NSMutableString* someString = [NSMutableString stringWithString: @"Hello"];
[someString appendString: @", world!"];

For example. Be more specific if this is not the answer you are looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.