Re: Trapping line breaks with a scanner
Re: Trapping line breaks with a scanner
- Subject: Re: Trapping line breaks with a scanner
- From: Itrat Khan <email@hidden>
- Date: Sun, 17 Mar 2002 14:48:48 -0500
On Sunday, March 17, 2002, at 02:08 AM, Zac Belado wrote:
I am trying to build an NSCharacterSet with all three possible line
breaks
in it. Am I correct in assuming that the following NSCharacterSet
NSCharacterSet *lineBreaks = [NSCharacterSet
characterSetWithCharactersInString:@"\r\n"];
will still catch Windows line breaks (\rn)?
Take a look at NSString's getLineStart:end:contentsEnd:range: instead,
which considers the various newline scenarios for you. (My documentation
incorrectly lists it as a factory method, but it is in fact an instance
method). You can then use substringWithRange: to extract the line. For
example:
[myString getLineStart:&lineStart end:&nextLineStart
contentsEnd:&lineEnd forRange:NSMakeRange(0, 0)];
lineRange = NSMakeRange(lineStart, lineEnd - lineStart);
line = [myString substringWithRange:lineRange];
The NSMakeRange(0, 0) parameter tells getLineStart: to search for the
line that encompasses the given range (so from the start of the string
to the next line). To process one line at a time, always supply a length
of zero.
The first time I used getLineStart:, I had to step through the code with
the debugger to understand what it was doing.
Regards,
Itrat.
..................................................................
Itrat Khan
Modeless Software, Inc.
http://www.modeless.com
_______________________________________________
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.