Created
August 22, 2012 16:20
-
-
Save pkh/3427169 to your computer and use it in GitHub Desktop.
An Objective-C function for parsing a comma-delimited line of text while preserving commas located within fields. End of the line is detected with either a '\n' newline character or the end of the character stream.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSMutableArray *)parseCSVStringIntoArray:(NSString *)csvString { | |
NSMutableArray *csvDataArray = [[NSMutableArray alloc] init]; | |
// break string into an array of individual characters | |
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[csvString length]]; | |
int csvStringLength = [csvString length]; | |
for (int c=0; c < csvStringLength; c++) { | |
NSString *ichar = [NSString stringWithFormat:@"%c", [csvString characterAtIndex:c]]; | |
[characters addObject:ichar]; | |
} | |
BOOL quotationMarksPresent = FALSE; | |
for (NSString *ichar in characters) { | |
if ([ichar isEqualToString:@"\""]) { | |
quotationMarksPresent = TRUE; | |
break; | |
} | |
} | |
if (!quotationMarksPresent) { | |
// quotation marks are NOT present | |
// simply break by comma and return | |
NSArray *componentArray = [csvString componentsSeparatedByString:@","]; | |
csvDataArray = [NSMutableArray arrayWithArray:componentArray]; | |
return csvDataArray; | |
} else { | |
// quotation marks ARE present | |
NSString *field = [NSString string]; | |
BOOL ignoreCommas = FALSE; | |
int counter = 0; | |
for (NSString *ichar in characters) { | |
if ([ichar isEqualToString:@"\n"]) { | |
// end of line reached | |
[csvDataArray addObject:field]; | |
return csvDataArray; | |
} | |
if (counter == ([characters count]-1)) { | |
// end of character stream reached | |
// add last character to field, add to array and return | |
field = [field stringByAppendingString:ichar]; | |
[csvDataArray addObject:field]; | |
return csvDataArray; | |
} | |
if (ignoreCommas == FALSE) { | |
if ( [ichar isEqualToString:@","] == FALSE) { | |
// ichar is NOT a comma | |
if ([ichar isEqualToString:@"\""] == FALSE) { | |
// ichar is NOT a double-quote | |
field = [field stringByAppendingString:ichar]; | |
} else { | |
// ichar IS a double-quote | |
ignoreCommas = TRUE; | |
field = [field stringByAppendingString:ichar]; | |
} | |
} else { | |
// comma reached - add field to array | |
if ([field isEqualToString:@""] == FALSE) { | |
[csvDataArray addObject:field]; | |
field = @""; | |
} | |
} | |
} else { | |
if ([ichar isEqualToString:@"\""] == FALSE) { | |
// ichar is NOT a double-quote | |
field = [field stringByAppendingString:ichar]; | |
} else { | |
// ichar IS a double-quote | |
// closing double-quote reached | |
ignoreCommas = FALSE; | |
field = [field stringByAppendingString:ichar]; | |
// end of field reached - add field to array | |
if ([field isEqualToString:@""] == FALSE) { | |
[csvDataArray addObject:field]; | |
field = @""; | |
} | |
} | |
} // END if (ignoreCommas == FALSE) | |
counter++; | |
} // END for (NSString *ichar in characters) | |
} | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment