Concatenate String in String Objective-c

Objective CIosString

Objective C Problem Overview


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!

Objective C Solutions


Solution 1 - Objective C

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"

Solution 2 - Objective C

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"];

Solution 3 - Objective C

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

or simply -[NSString stringByAppendingString:]

Solution 4 - Objective C

You would normally use -stringWithFormat here.

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

Solution 5 - Objective C

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"

Solution 6 - Objective C

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.

Solution 7 - Objective C

simple one:

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjsttnView Question on Stackoverflow
Solution 1 - Objective CDustinView Answer on Stackoverflow
Solution 2 - Objective Cuser529758View Answer on Stackoverflow
Solution 3 - Objective CjustinView Answer on Stackoverflow
Solution 4 - Objective CBill BurgessView Answer on Stackoverflow
Solution 5 - Objective CYujiView Answer on Stackoverflow
Solution 6 - Objective CRodrigoView Answer on Stackoverflow
Solution 7 - Objective CfengdView Answer on Stackoverflow