splitting lines
splitting lines
- Subject: splitting lines
- From: Koen van der Drift <email@hidden>
- Date: Thu, 10 Jul 2003 21:32:15 -0400
Hi,
To split a text file into separate lines, I use the following category
for NSMutableString:
(I got this somewhere from the archives)
-(NSMutableArray *)splitLines
{
NSMutableArray *arrayOfLines = [[NSMutableArray alloc] init];
unsigned stringLength = [self length];
NSRange lineRange = NSMakeRange(0, 0);
NSRange searchRange = NSMakeRange(0, 0);
while ( searchRange.location < stringLength )
{
[self getLineStart:nil end: &searchRange.location contentsEnd: nil
forRange: searchRange];
lineRange.length = searchRange.location - lineRange.location;
[arrayOfLines addObject:[self substringWithRange: lineRange]];
lineRange.location = searchRange.location;
}
return [arrayOfLines autorelease];
}
I tried componentsByString first, but that didn;t work in my case,
because the line-ending can be '\r' or '\n', and componentsByString
only can use one. The problem is that when there are many lines (a few
thousand) the code above is very slow.
Is there a faster way to do this, or a way to improve the above code?
thanks,
- Koen.
_______________________________________________
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.