Datenshi
Datenshi

Reputation: 1191

Adding text in NSString in first place

I need to add constant text to NSString and pass it to my method. The idea is that i need to have "X1"and afterwards goes string which is dynamic. How should i fuse them to one string?

Well, here is the code, maybe it will be much clearer than my explanation of that I am trying to do.

 NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"Name"];
 [self sendSMS:[@"*X1*%@" name]];

Upvotes: 0

Views: 112

Answers (2)

ggrana
ggrana

Reputation: 2345

You have a lot of options to do that

You can use NSString stringWithFormat, for example.

[NSString stringWithFormat:@"X1%@", name];

But I really advise you to study the NSString class to know what is the best choice for you. Not only for this case, but for all the future cases that you will face developing.

Some option to your code are:

NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"Name"];
[self sendSMS:[NSString stringWithFormat:@"X11%@",name]];

or

NSMutableString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"Name"];
[name insertString:@"X11" atIndex:0];
[self sendSMS:name];

Upvotes: 3

Mick MacCallum
Mick MacCallum

Reputation: 130222

I'm not 100% clear on what you're asking but does this help?

NSString *name = [@"X1" stringByAppendingString:[[NSUserDefaults standardUserDefaults] stringForKey:@"Name"]];
[self sendSMS:name];

Upvotes: 3

Related Questions