Re: [newbie] Reading a CSV file
Re: [newbie] Reading a CSV file
- Subject: Re: [newbie] Reading a CSV file
- From: Dan Wood <email@hidden>
- Date: Fri, 14 Jun 2002 16:32:51 -0700
OK, I've risen to the challenge. Here's an NSString category
method, componentsSeparatedByLineSeparators, that splits a
string into lines, regardless of line separator. Share and
enjoy!
Dan Wood
/*" Split a string into lines separated by any of the various
newline characters. Equivalent to
componentsSeparatedByString:@"\n" but it works with the
different line separators: \r, \n, \r\n, 0x2028, 0x2029
"*/
- (NSArray *) componentsSeparatedByLineSeparators
{
NSMutableArray *result = [NSMutableArray array];
NSRange range = NSMakeRange(0,0);
unsigned start, end, contentsEnd;
while (contentsEnd < [self length])
{
[self getLineStart:&start end:&end
contentsEnd:&contentsEnd forRange:range];
[result addObject:[self
substringWithRange:NSMakeRange(start,contentsEnd-start)]];
range.location = contentsEnd+1;
range.length = 0;
}
return result;
}
FROM: Itrat Khan
DATE: 2002-06-04 23:01
On Sunday, June 2, 2002, at 11:18 PM, Dan Wood wrote:
> Here's some code that I am working on, to appear in a future MacTech
> article. Feel free to make use of it (and comment on it)!
>
> NSArray *lines = [self
componentsSeparatedByString:@"\n"];
To separate a text file by lines, use NSString's
getLineStart:end:contentsEnd:range instead. It's only a little more
work, but it will properly handle various line endings for you (UNIX,
Mac, DOS, etc.).
Regards.
..................................................................
Itrat Khan
Modeless Software, Inc.
http://www.modeless.com
--
Dan Wood
Karelia Software, LLC
email@hidden
http://www.karelia.com/
Watson for Mac OS X:
http://www.karelia.com/watson/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.