Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge 5 NSStrings in Objective-C

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=\"...\">My Cool App for iPhone</a> in just a few seconds!<p><b><a href=\""+newURL+@"\">"+newURL+@"</a></b></p>";
like image 907
tarnfeld Avatar asked Nov 22 '25 04:11

tarnfeld


1 Answers

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"

like image 122
Thomas Zoechling Avatar answered Nov 23 '25 19:11

Thomas Zoechling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!