I am trying to parse a String to an Array each item is between <> for example <this is column 1><this is column 2> etc....
Help would be much appreciated.
Thanks
I am trying to parse a String to an Array each item is between <> for example <this is column 1><this is column 2> etc....
Help would be much appreciated.
Thanks
Something to demonstrate:
NSString *string = @"<this is column 1><this is column 2>";
NSScanner *scanner = [NSScanner scannerWithString:string];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
NSString *temp;
while ([scanner isAtEnd] == NO)
{
// Disregard the result of the scanner because it returns NO if the
// "up to" string is the first one it encounters.
// You should still have this in case there are other characters
// between the right and left angle brackets.
(void) [scanner scanUpToString:@"<" intoString:NULL];
// Scan the left angle bracket to move the scanner location past it.
(void) [scanner scanString:@"<" intoString:NULL];
// Attempt to get the string.
BOOL success = [scanner scanUpToString:@">" intoString:&temp];
// Scan the right angle bracket to move the scanner location past it.
(void) [scanner scanString:@">" intoString:NULL];
if (success == YES)
{
[array addObject:temp];
}
}
NSLog(@"%@", array);
NSString *input =@"<one><two><three>";
NSString *strippedInput = [input stringByReplacingOccurencesOfString: @">" withString: @""]; //strips all > from input string
NSArray *array = [strippedInput componentsSeperatedByString:@"<"];
Note that [array objectAtIndex:0] will be an empty string ("") an this doesn't work of course, if one of the "actual" string contain < or >
One approach might be to use either componentsSeparatedByCharactersInSet or componentsSeparatedByString from NSString.
NSString *test = @"<one> <two> <three>";
NSArray *array1 = [test componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
NSArray *array2 = [test componentsSeparatedByString:@"<"];
You'll need to do some cleaning up afterward, either trimming in the case of array2 or removing white-space strings in the case of array1