4

I am trying to to do the following (psuedo)

NSString *variable = 'Name'; 
NSString *newString = @"Hello, " + variable + @" blah blah";

It would appear it is not as simple as the above! Any help?

Thanks

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
user1202278
  • 774
  • 2
  • 10
  • 24
  • 2
    Possible duplicate of [How do I concatenate strings in Objective-C?](http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c) – Philipp Schlösser Jul 07 '12 at 15:07
  • 1
    Please rename this question; it doesn't really have anything to do with Xcode (Xcode is the IDE, this is an Objective-C and Cocoa question). – Kevin Grant Jul 07 '12 at 15:07

2 Answers2

9
NSString *str = @"Hello, ";
str = [str stringByAppendingString:variable];
str = [str stringByAppendingString:@" blah blah"];

If you prefer it as one line statement

 NSString *str = [[@"Hello, " stringByAppendingString:variable] stringByAppendingString:@" blah blah"];

Swift

var str = "Hello, " + varaible + "blah blah"

OR

var str = "Hello, \(variable) blah blah"
msk
  • 8,885
  • 6
  • 41
  • 72
4
NSString *newString = [NSString stringWithFormat:@"Hello, %@ blah blah", variable];
MattR
  • 6,908
  • 2
  • 21
  • 30