Split NSString several times on the same splitter

I am currently getting a line like this:

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

And I break 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]]; } 

Now I get a line 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 split a line more than once with "|" Separator?

+68
string objective-c cocoa-touch cocoa nsstring
Jun 10 2018-11-11T00:
source share
2 answers

The next line ...

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

will cause the array to now contain 3 elements instead of 2 ..... no need to share again!

+161
Jun 10 '11 at 9:30 a.m.
source share

follow these steps.

 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 to save an array.

+4
Jun 10 2018-11-11T00:
source share



All Articles