Blog Archives

Concatenate string in Objective-C

Unfortunately, this is not as straight forward as it seems it should be. This really comes down to roughly two approaches in my opinion:

stringByAppendingString approach:

NSString *robot= @"Ronnie";
NSString *robotname = [robot stringByAppendingString:@" is the name of a robot."];

strigWithFormat approach:

NSString *robot= @"Ronnie";
NSString *robotname= @" is the name of the a robot.";
NSString *robotknowledge = @" knows eight languages.";
[NSString stringWithFormat:@"%@/%@/%@", robot, robotname, robot, robotknowledge];

StackOverflow,, “Learn Objective-C”,