Objective-C - 由管道分隔的串联字符串

时间:2011-03-07 14:05:33

标签: iphone objective-c nsstring string-concatenation

我想循环遍历一个数组字符串,并按以下方式将它们添加到NSString

NSMutableArray *emailsArray = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];

for (id email in emailsArray {
    NSString *emails = ??; 
}

所以最后的NSString应该如下:

NSString *emails = @"One|Two|Three";

1 个答案:

答案 0 :(得分:7)

请使用[emailsArray componentsJoinedByString:@"|"]

样品:

NSMutableArray *emailsArray = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
NSString *emails = [emailsArray componentsJoinedByString:@"|"];

在这里,您将拥有emails = @"One|Two|Three"