52

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!

jsttn
  • 1,495
  • 3
  • 16
  • 21
  • Lots of answers below, [here](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:) is the documentation on stringWithFormat. – Joachim Isaksson Aug 15 '12 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"];` –  Aug 15 '12 at 15:16
  • @rdelmar not really. All of these answers are perfectly valid and should be upvoted. –  Aug 15 '12 at 16:03
  • With the improvements they added with quickly initializing NSDictionary and NSArray, I'm surprised they haven't simplified string concatenation yet. – William T. May 14 '13 at 17:22

7 Answers7

123

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"
Dustin
  • 6,783
  • 4
  • 36
  • 53
  • Would this work for numerous strings? Like have several %@ and do varyingString, varyingString2? – jsttn Aug 15 '12 at 15:14
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"];
10
NSString * varyingString = ...;
NSString * cat = [NSString stringWithFormat:@"%s%@%@",
  "first part of string",
  varyingString,
  @"third part of string"];

or simply -[NSString stringByAppendingString:]

justin
  • 104,054
  • 14
  • 179
  • 226
6

You would normally use -stringWithFormat here.

NSString *myString = [NSString stringWithFormat:@"%@%@%@", @"some text", stringVariable, @"some more text"];
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
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"
Yuji
  • 34,103
  • 3
  • 70
  • 88
3

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.

Rodrigo
  • 674
  • 8
  • 19
1

simple one:

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

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

fengd
  • 7,551
  • 3
  • 41
  • 44