Split NSString multiple times on the same separator

前端 未结 2 1143
借酒劲吻你
借酒劲吻你 2020-12-03 00:22

I am currently receiving a string like this:

@\"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54\"

And I am splitting it like this:



        
相关标签:
2条回答
  • 2020-12-03 00:51

    The following line...

    testArray2 = [s componentsSeparatedByString:@"|"];
    

    will cause the array to now contain 3 items, instead of 2..... no need to split again!

    0 讨论(0)
  • 2020-12-03 01:04

    do like this.

    NSString *testString = [[NSString alloc] initWithFormat:@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30"];
        NSArray *testArray = [testString componentsSeparatedByString:@","];
        NSLog(@"%@",testArray);
        for(int i=0;i<[testArray count];i++){
            NSString *str=[testArray objectAtIndex:i];
        NSArray *aa=[str componentsSeparatedByString:@"|"];
        NSLog(@"%@",aa);
        }
    

    No need of retain the array.

    0 讨论(0)
提交回复
热议问题