在Objective-C中合并5个字符串

在Objective-C中合并5个字符串,objective-c,merge,nsstring,Objective C,Merge,Nsstring,我有多个NSStrings,我想把它们合并成一个,这是我目前的代码 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&

我有多个
NSStrings
,我想把它们合并成一个,这是我目前的代码

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>";
NSString*newURL=[\u参数objectForKey:@“url”];
NSString*emailBody=@“嘿!
我刚在几秒钟内用剪断了我的长url!

”;
您可以试试

NSString *emailBody = [ NSString stringWithFormat: @"Hey!<br>I just snipped my long url with <a href=\"http://itunes.com/app/SnippetySnip\">Snippety Snip for iPhone</a> in just a few seconds, why not check it out?<p><b><a href=\"%@\">%@</a></b></p>", newURL ];
NSString*emailBody=[NSString stringWithFormat:@“嘿!
我刚在几秒钟内剪下了我的长url,为什么不签出来呢?

”,newURL];
具有更好的可读性和更少的错误倾向:

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\">Snippety Snip for iPhone</a> in just a few seconds, why not check it out?<p><b><a href=\"%@\">%@</a></b></p>", newUrl, newUrl];
NSString*newURL=[\u参数objectForKey:@“url”];
NSString*emailBody=[NSString stringWithFormat:@“嘿!
我刚在几秒钟内剪下了我的长url,为什么不签出来?,newUrl,newUrl];
您可以使用以下方法连接Cocoa中的字符串:

[NSString stringByAppendingString:]


或者您可以使用[NSString stringWithFormat]方法,该方法允许您指定一个带有变量参数列表的C样式格式字符串来填充转义序列。

如果您知道现有字符串的数量,您可以直接对其进行编码:

NSString* longString = [firstString stringByAppendingString:secondString];
或:

如果您有任意数量的字符串,可以将它们放入NSArray中,并使用以下方法连接它们:

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


Cocoa中字符串处理的另一个好资源是:

鉴于您有多个字符串,我建议使用数组:

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

哈哈哈,我需要这方面的帮助-这到底是什么垃圾邮件?我认为这个问题还可以,而iTunes中搜索的“提示”却不行。
NSArray *array = [NSArray arrayWithObjects:@"URL", @"person", "body"];
NSString *combined = [array componentsJoinedByString:@""];