Re: NSScanner searches from start
Re: NSScanner searches from start
- Subject: Re: NSScanner searches from start
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 25 Jun 2001 01:21:35 -0700
On Sunday, June 24, 2001, at 10:41 PM, Ivan Myrvold wrote:
But if I put the statement:
NSString *animal = @"Dog";
the scanUpToString:intoString also returns YES, even if the string
"Dog" is not a part of the string. Is that a bug, then?
No, that's not a bug, because according to the docs:
- (BOOL)scanUpToString:(NSString *)stopString
intoString:(NSString **)stringValue
Scans the string until stopString is encountered, accumulating
characters into a string that's returned by reference in stringValue.
Returns YES if the receiver scans any characters; otherwise returns NO.
So, if stopString isn't encountered, characters still get scanned. In
fact, *all* of the characters get scanned.
-jcr
Ivan
On Sunday, June 24, 2001, at 03:51 PM, John C. Randolph wrote:
On Sunday, June 24, 2001, at 04:39 AM, Ivan Myrvold wrote:
I have just started to use NSScanner, and it obviously only finds
patterns from start. Here is an example:
NSScanner *myScanner;
NSString *animal = @"Cow";
myScanner = [NSScanner scannerWithString:@"ChickenAndTheCow"];
if([myScanner scanString:animal intoString:NULL]) {
NSLog(@"Animal");
} else {
NSLog(@"Not an animal");
}
This gives the result "Not an animal", but if I initialize NSString
*animal with "Chicken", this results in "Animal" in the log.
How can I get NSScanner to also recognize "Cow" in the string?
What you need in this case is -scanUpToString:, not -scanString:.
I also have a problem to understand what the parameter intoString:
does.
The parameter after intoString: is where the method will put whatever
characters it scanned.
For example:
NSString *firstNameAndInitial
NSScanner *nameScanner = [NSScanner scannerWithString:@"John C.
Randolph"];
[nameScanner scanUpToString:@"Randolph"
intoString:firstNameAndInitial];
at this point, firstNameAndInitial points to a string equal to @"John
C. "
-jcr
For every complex problem there is an answer that is clear, simple, and
wrong. -- H L Mencken