Split NSString multiple times on the same separator

67,289

Solution 1

The following line...

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

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

Solution 2

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.

Share:
67,289

Related videos on Youtube

Sam Parrish
Author by

Sam Parrish

Updated on July 05, 2022

Comments

  • Sam Parrish
    Sam Parrish almost 2 years

    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:

    testArray = [[NSArray alloc] init];
    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"];
    testArray = [testString componentsSeparatedByString:@","];
    
    dict = [NSMutableDictionary dictionary];
    for (NSString *s in testArray) {
    
        testArray2 = [s componentsSeparatedByString:@"|"];
        [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
    }
    

    I will now be receiving a string like this:

    @"Sam|26|Developer,Hannah|22|Team Leader,Adam|30|Director,Carlie|32|PA,Jan|54|Cleaner"
    

    Can I (and how) use the same method as above to separate the string more than once using the "|" separator?

    • Mark Amery
      Mark Amery over 10 years
      Somehow I suspect that almost all of the viewers and upvoters on this question and its answer were people simply looking for NSString's 'split' method, and discovering the answer here incidentally.
    • boxed
      boxed about 10 years
      The first line is just creating an NSArray that you throw away at line 3.
  • Sam Parrish
    Sam Parrish almost 13 years
    great thanks! just need to figure out how to create and show the UITableViewCell with 3 labels...
  • Simon Lee
    Simon Lee almost 13 years
    You can either subclass UITableViewCell and do everything manually OR you can use one of the preset table view cell styles and use the content view to add an extra label. The apple guide details it all....
  • Simon Lee
    Simon Lee almost 13 years
  • Simon Lee
    Simon Lee almost 13 years
    Check out the 'Customizing Cells' part in 'A closer look at table view cells' section
  • Tendulkar
    Tendulkar almost 13 years
    Why I have written the code is In his code he has written the retain statement.No need of it.
  • Sam Parrish
    Sam Parrish almost 13 years
    I have created the custom tableviewcell and used the same code, but when i configure the cell, what do i put for the third label?
  • pfrank
    pfrank over 10 years
    don't use initWithFormat if not using a format? just: NSString *testString = @"blahblah";