how to add concatenate multiple NSString in one String in iphone

喜夏-厌秋 提交于 2019-11-30 09:02:00
NSArray *myStrings = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
NSString *joinedString = [myStrings componentsJoinedByString:@"|"];
// release myStrings if not using ARC.
you can try ....
NSString *joinString=[NSString stringWithFormat:@"%@|%@|%@|%@|%@",youstring1,youstring2,youstring3,youstring4,youstring5];

Short solution:

NSString *str = [@[nstring1, nstring2, nstring3] componentsJoinedByString:@","];

I guess what DrummerB suggested, is the best way. You have to store the strings in data structure. Array or dictionary for that matter.
If you just want to use strings it is not impossible, but it will be unwise. Here you go :

NSString*first=@"Ali";  
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"Imran"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"AliImran"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"ImranAli"];
first = [first stringByAppendingString:@"|"];
first = [first stringByAppendingString:@"Ali Imran Jamshed"];
NSArray *stringsArray = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
NSString *combinedString = [stringsArray componentsJoinedByString:@","];

The combined String looks like this @"Ali,Imran,AliImran,ImranAli,Ali Imran Jamshed";

To improve on Nitish's answer, you can reduce the number of lines by following this:

NSString *first=@"Ali";  
first = [first stringByAppendingString:[@"|" stringByAppendingString:[@"Imran" stringByAppendingString:@"|"]]];
.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!