Re: NSScanner problem...
Re: NSScanner problem...
- Subject: Re: NSScanner problem...
- From: Stephan Lichtenauer <email@hidden>
- Date: Sun, 3 Aug 2003 10:21:32 +0200
Jeff,
this does not exactly answer your question, but there is an (in my
eyes...) easier solution to split a string line after line:
NSString *file = [NSString stringWithContentsOfFile:@"FILE.TXT"];
NSArray *arrayWithLines = [file componentsSeparatedByString:@"\n"];
This should give you an array with one NSString per line.
So (I have not tried this but it should work) if you search for "\n\n"
for example you should get exactly the header as the first array item,
which you then could split further into single lines or whatever you
want to do with it.
Cheers
Stephan
Am Sonntag, 03.08.03, um 02:54 Uhr (Europe/Berlin) schrieb Jeff
LaMarche:
>
I'm trying to use an NSScanner to process the contents of an NSString
>
line by line. The problem is that it seems to skip empty lines, which
>
is a problem since blank lines separate sections. Here's my code,
>
which is from a category on NSString - I'm trying to break out of the
>
loop when I get to an empty line because I only want the header:
>
>
-(NSString *)parseHeader
>
{
>
NSMutableString *ret = [NSMutableString stringWithString:@""];
>
NSScanner *scanner = [NSScanner scannerWithString:self];
>
NSCharacterSet *newlineSet = [NSCharacterSet
>
characterSetWithCharactersInString:@"\r\n"];
>
NSCharacterSet *whitespaceSet = [NSCharacterSet
>
whitespaceCharacterSet];
>
>
while ([scanner isAtEnd] == NO)
>
{
>
NSString *oneLine = nil;
>
[scanner scanUpToCharactersFromSet:newlineSet intoString:&oneLine];
>
oneLine = [oneLine stringByTrimmingCharactersInSet:whitespaceSet];
>
if ([oneLine isEqualTo:@""])
>
return ret;
>
>
[ret appendString:oneLine];
>
}
>
return ret;
>
}
>
>
So, if I have an NSString that looks like this:
>
>
one line of text.
>
A second line of text.
>
A third line of text.
>
>
A fourth line of text.
>
>
My loop never sees the blank line.. it just skips over if it as if it
>
didn't exist. I saw a reference on the mailing list to using the
>
setCharactersToBeSkipped method of NSScanner, like so:
>
>
[theScanner setCharactersToBeSkipped:[NSCharacterSet
>
emptyCharacterSet]];
>
>
but emptyCharacterSet is not a delivered class method and when I tried
>
to simulate the behavior like this:
>
>
[scanner setCharactersToBeSkipped:[NSCharacterSet
>
characterSetWithCharactersInString:@""]];
>
>
But got a SIGTRAP..
>
>
If anyone has any thoughts, I'd love to hear them, as I'm stumped and
>
am about to abandon NSScanner in favor of some other solution. I could
>
understand having the opposite problem if my string had CRLFs, but I
>
can't figure out why this would be happening.
>
>
Thanks as always,
>
Jeff
>
_______________________________________________
>
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.
_______________________________________________
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.