how to add concatenate multiple NSString in one String in iphone

后端 未结 6 1310
有刺的猬
有刺的猬 2020-12-31 08:19

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

   NSString *first=@\"Ali\";
   NSString *         


        
相关标签:
6条回答
  • 2020-12-31 08:50

    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:@"|"]]];
    .
    
    0 讨论(0)
  • 2020-12-31 09:00
    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";

    0 讨论(0)
  • 2020-12-31 09:02
    NSArray *myStrings = [[NSArray alloc] initWithObjects:first, second, third, fourth, fifth, nil];
    NSString *joinedString = [myStrings componentsJoinedByString:@"|"];
    // release myStrings if not using ARC.
    
    0 讨论(0)
  • 2020-12-31 09:02

    Short solution:

    NSString *str = [@[nstring1, nstring2, nstring3] componentsJoinedByString:@","];
    
    0 讨论(0)
  • 2020-12-31 09:12

    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"];
    
    0 讨论(0)
  • 2020-12-31 09:14
    you can try ....
    NSString *joinString=[NSString stringWithFormat:@"%@|%@|%@|%@|%@",youstring1,youstring2,youstring3,youstring4,youstring5];
    
    0 讨论(0)
提交回复
热议问题