how to add concatenate multiple NSString in one String in iphone

亡梦爱人 提交于 2019-11-29 13:15:37

问题


I have 5 String i want that they must be store in singe NSString all the values separate with | sign

   NSString *first=@"Ali";
   NSString *second=@"Imran";
   NSString *third=@"AliImran";
   NSString *fourth=@"ImranAli";
   NSString *fifth=@"Ali Imran Jamshed";

I want to all these in single NSString to store and all values separated by given sign.


回答1:


NSArray *myStrings = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
NSString *joinedString = [myStrings componentsJoinedByString:@"|"];
// release myStrings if not using ARC.



回答2:


you can try ....
NSString *joinString=[NSString stringWithFormat:@"%@|%@|%@|%@|%@",youstring1,youstring2,youstring3,youstring4,youstring5];



回答3:


Short solution:

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



回答4:


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"];



回答5:


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";




回答6:


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:@"|"]]];
.


来源:https://stackoverflow.com/questions/12156780/how-to-add-concatenate-multiple-nsstring-in-one-string-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!