user1202278
user1202278

Reputation: 773

Objective C insert variable between 2 strings concat

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

Upvotes: 4

Views: 9652

Answers (2)

msk
msk

Reputation: 8905

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"

Upvotes: 9

MattR
MattR

Reputation: 7048

NSString *newString = [NSString stringWithFormat:@"Hello, %@ blah blah", variable];

Upvotes: 4

Related Questions