• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [newbie] Reading a CSV file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: NSFileHandleReadCompletionNotification and sockets
  • Next by Date: Determining a file's "kind"
  • Previous by thread: Re: [newbie] Reading a CSV file
  • Next by thread: sheet window on a custom window
  • Index(es):
    • Date
    • Thread