Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
- Subject: Re: NSScanner baffling behaviour (scanString:intoString: doesn't work!)
- From: Greg Hurrell <email@hidden>
- Date: Wed, 9 Apr 2003 00:36:09 +0930
El Miircoles, 9 abr, 2003, a las 00:04 Australia/Adelaide, j o a r
escribis:
My code snipped was just quickly hacked together in Mail and not
tested at all - but I don't think it would fail the scenario that you
describe. I don't see why "scanUpToString:" wouldn't work if the
search string is at the beginning of the scanned string. Please test
it and let me know if you find that I'm wrong.
Testing shows that it won't pick up the "startTag" if it's at the very
beginning of the string being scanned. The reason for this is that in
that case scanUpToString returns "NO". (The docs say, "Returns YES if
the receiver scans any characters; otherwise returns NO.") I've tested
it both ways, and it only works if it's not at the very beginning.
The following code works in either case... It'll return nil if either
tag is missing, or if nothing is found between them. Haven't yet tested
what would happen if someone passed nil strings or empty strings (@"")
for startTag and/or endTag. Thanks for your help!
+ (NSString *)_scanString:(NSString *)source
startTag:(NSString *)startTag
endTag:(NSString *)endTag
{
NSScanner *scanner = [NSScanner scannerWithString:source];
NSString *result;
[scanner scanUpToString:startTag intoString:nil];
if ([scanner scanString:startTag intoString:nil])
{
if ([scanner scanUpToString:endTag intoString:&result])
return result;
}
return nil;
}
_______________________________________________
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.