Skip navigation.
 
mlRe: Slice a .txt file in lines
FROM : Sean Murphy
DATE : Tue Jun 20 17:13:22 2006

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

Related mailsAuthorDate
mlSlice a .txt file in lines Neto Jun 20, 07:44
mlRe: Slice a .txt file in lines Ben Lachman Jun 20, 08:11
mlRe: Slice a .txt file in lines Andreas Mayer Jun 20, 14:38
mlRe: Slice a .txt file in lines Sean Murphy Jun 20, 17:13
mlRe: Slice a .txt file in lines Andreas Mayer Jun 20, 17:44
mlRe: Slice a .txt file in lines Douglas Davidson Jun 20, 17:48
mlRe: Slice a .txt file in lines Sean Murphy Jun 20, 19:44
mlRe: Slice a .txt file in lines Neto Jun 21, 05:59