Re: Slice a .txt file in lines
Re: Slice a .txt file in lines
- Subject: Re: Slice a .txt file in lines
- From: Douglas Davidson <email@hidden>
- Date: Tue, 20 Jun 2006 08:48:57 -0700
On Jun 20, 2006, at 8:13 AM, Sean Murphy wrote:
Here's yet another line-ending-independent way I've been using to
split an NSString of lines into an NSArray. I just thought I'd
share it in addition to the previous methods given..
Assumptions..
NSString *textFileAsString (entire text file in one large string)
NSArray *linesInTextFile (each line as an array element)
// Finds the first character past the line terminator and the first
character of the line terminator, respectively
unsigned int endOfLineIndex, startOfLineTerminatorIndex;
// Determine key locations of the first line in the file:
[textFileAsString getLineStart:NULL end:&endOfLineIndex
contentsEnd:&startOfLineTerminatorIndex forRange:NSMakeRange(0,0)];
// (Passing NULL will skip computation for any unneeded values)
// (Takes the entire line in which the range is found, thus passing
the above range picks out the first line)
// Finds the line ending ending character:
NSString *lineEndCharacter = [textFileAsString
substringWithRange:NSMakeRange(startOfLineTerminatorIndex,
endOfLineIndex-startOfLineTerminatorIndex)];
// Use the line ending character to split the access log into an
array containing each line as a string object
NSArray *linesInTextFile = [textFileAsString
componentsSeparatedByString:lineEndCharacter];
This code assumes that the file uses a single line terminator
consistently throughout. Andreas' method will use whatever
terminators occur in the file.
One other note is that there are two sets of methods: one that uses
the word "line", and another that uses the word "paragraph". The
distinction is because there are line separators that are not
paragraph separators, U+0085 NEXT LINE and U+2028 LINE SEPARATOR.
This distinction may or may not be important depending on what you
are trying to do.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden