Re: Slice a .txt file in lines
Re: Slice a .txt file in lines
- Subject: Re: Slice a .txt file in lines
- From: Sean Murphy <email@hidden>
- Date: Tue, 20 Jun 2006 11:13:22 -0400
On Jun 20, 2006, at 8:38 AM, Andreas Mayer wrote:
Am 20.06.2006 um 08:11 Uhr schrieb Ben Lachman:
particularly componentsSeparatedByString: is useful for splitting
files by line endings.
Not really, since there is more than one line ending character. Try
this one instead:
[...]
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];
Documentation for NSString getLine[...] method:
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSString_Class/Reference/Reference.html#//apple_ref/occ/instm/
NSString/getLineStart:end:contentsEnd:forRange:>
- Murph
_______________________________________________
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