Re: NSScanner searches from start
Re: NSScanner searches from start
- Subject: Re: NSScanner searches from start
- From: "John C. Randolph" <email@hidden>
- Date: Sun, 24 Jun 2001 04:51:21 -0700
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
"Scientology is both immoral and socially obnoxious... it is
corrupt, sinister and dangerous." - Mr. Justice Latey, London 1984