53

I want to place a string within a string. Basically in pseudo code:

"first part of string" + "(varying string)" + "third part of string"

How can I do this in objective-c? Is there a way to easily concatenate in obj-c? Thanks!

4
  • Lots of answers below, here is the documentation on stringWithFormat. Commented Aug 15, 2012 at 15:13
  • 4
    [NSString stringWithFormat:@"Just google '%@'!", @"http://www.google.hu/search?q=nsstring+class+reference&ie=UTF-8&oe=UTF-8&hl=en-gb&client=safari"]; Commented Aug 15, 2012 at 15:16
  • @rdelmar not really. All of these answers are perfectly valid and should be upvoted. Commented Aug 15, 2012 at 16:03
  • With the improvements they added with quickly initializing NSDictionary and NSArray, I'm surprised they haven't simplified string concatenation yet. Commented May 14, 2013 at 17:22

7 Answers 7

125

Yes, do

NSString *str = [NSString stringWithFormat: @"first part %@ second part", varyingString];

For concatenation you can use stringByAppendingString

NSString *str = @"hello ";
str = [str stringByAppendingString:@"world"]; //str is now "hello world"

For multiple strings

NSString *varyingString1 = @"hello";
NSString *varyingString2 = @"world";
NSString *str = [NSString stringWithFormat: @"%@ %@", varyingString1, varyingString2];
//str is now "hello world"
Sign up to request clarification or add additional context in comments.

1 Comment

Would this work for numerous strings? Like have several %@ and do varyingString, varyingString2?
13

Variations on a theme:

NSString *varying = @"whatever it is";
NSString *final = [NSString stringWithFormat:@"first part %@ third part", varying];

NSString *varying = @"whatever it is";
NSString *final = [[@"first part" stringByAppendingString:varying] stringByAppendingString:@"second part"];

NSMutableString *final = [NSMutableString stringWithString:@"first part"];
[final appendFormat:@"%@ third part", varying];

NSMutableString *final = [NSMutableString stringWithString:@"first part"];
[final appendString:varying];
[final appendString:@"third part"];

Comments

10
NSString * varyingString = ...;
NSString * cat = [NSString stringWithFormat:@"%s%@%@",
  "first part of string",
  varyingString,
  @"third part of string"];

or simply -[NSString stringByAppendingString:]

Comments

6

You would normally use -stringWithFormat here.

NSString *myString = [NSString stringWithFormat:@"%@%@%@", @"some text", stringVariable, @"some more text"];

1 Comment

This is really the best way when you have a large string that you are building for let's say an XML.
4

Iam amazed that none of the top answers pointed out that under recent Objective-C versions (after they added literals), you can concatenate just like this:

@"first" @"second"

And it will result in:

@"firstsecond"

You can not use it with NSString objects, only with literals, but it can be useful in some cases.

Comments

3

Just do

NSString* newString=[NSString stringWithFormat:@"first part of string (%@) third part of string", @"foo"];

This gives you

@"first part of string (foo) third part of string"

Comments

1

simple one:

[[@"first" stringByAppendingString:@"second"] stringByAppendingString:@"third"];

if you have many STRINGS to Concatenate, you should use NSMutableString for better performance

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.