views:

701

answers:

6

I have multiple NSStrings and i wish to merge them into one other, here is my code so far...

NSString *newURL = [_parameters objectForKey:@"url"];
NSString *emailBody = @"Hey!<br>I just snipped my long url with <a href=\"http://itunes.com/app/SnippetySnip\"&gt;Snippety Snip for iPhone</a> in just a few seconds, why not check it out?<p><b><a href=\""+newURL+@"\">"+newURL+@"</a></b></p>";
+1  A: 

You can try

NSString *emailBody = [ NSString stringWithFormat: @"Hey!<br>I just snipped my long url with <a href=\"http://itunes.com/app/SnippetySnip\"&gt;Snippety Snip for iPhone</a> in just a few seconds, why not check it out?<p><b><a href=\"%@\">%@</a></b></p>", newURL ];
Jacob Relkin
where do i put this?
tarnfeld
thankkkk yooooooo
tarnfeld
A: 

Formatting string has better readability and less error-prone:

NSString *newURL = [_parameters objectForKey:@"url"];
NSString *emailBody = [NSString stringWithFormat:@"Hey!<br>I just snipped my long url with <a href=\"http://itunes.com/app/SnippetySnip\"&gt;Snippety Snip for iPhone</a> in just a few seconds, why not check it out?<p><b><a href=\"%@\">%@</a></b></p>", newUrl, newUrl];
Iamamac
A: 

You can concatenate strings in Cocoa using:

[NSString stringByAppendingString:]

Or you could use the [NSString stringWithFormat] method which will allow you to specify a C-style format string with a variable argument list to populate the escape sequences.

mikecsh
+5  A: 

If you know the number of your existing strings, you can just concat them:

NSString* longString = [firstString stringByAppendingString:secondString];

or:

NSString* longString = [NSString stringWithFormat:@"A string: %@, a float: %1.2f", @"string", 31415.9265];

If you have an arbitrary number of strings, you could put them in an NSArray and join them with:

NSArray* chunks  = ... get an array, say by splitting it;
NSString* string = [chunks componentsJoinedByString: @" :-) "];

(Taken from http://borkware.com/quickies/one?topic=NSString)

Another good resource for string handling in Cocoa is: "String Programming Guide"

weichsel
A: 

Given that you've got multiple strings I recommend using an Array:

NSArray *array = [NSArray arrayWithObjects:@"URL", @"person", "body"];
NSString *combined = [array componentsJoinedByString:@""];
Georg
A: 

hi all, i have a stupid question...

NSString *newURL = [_parameters objectForKey:@"url"];

when i use this i get error... here what is _parameters and how to declare it??

Rony
If you have question use the big "Ask question" button on the top - this isn't a forum.
Georg Fritzsche
sorry for my mistake....Georg...please look at the link...i think u know the answer and i will be glad if u answer...i know its a very stupid question but i can't figure it out....i am bad need of thisthankshttp://stackoverflow.com/questions/3274826/receive-data-from-javascript
Rony