• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: finding decimals in string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finding decimals in string


  • Subject: Re: finding decimals in string
  • From: Matt Neuburg <email@hidden>
  • Date: Sat, 25 Aug 2007 15:16:49 -0700
  • Thread-topic: finding decimals in string

On Fri, 24 Aug 2007 16:04:26 -0400, Daniel Child <email@hidden> said:
>Hi All,
>
>I have been looking at the NSScanner and NSCharacterSet classes for a
>way to find
>out whether a string contains digits. I thought something along these
>lines might work:
>
>/********************************************************* FUNCTIONAL
>METHODS **
>* evaluateText
>*
>* Evaluates whether the textInput field contains numbers and stores
>that result
>* in the hasNumbers instance variable.
>************************************************************************
>*******/
>- (void) evaluateText
>{
> NSCharacterSet *numberChars;
> NSScanner *scanner;
> NSMutableString *numbersFound;
>
> numberChars = [NSCharacterSet decimalDigitCharacterSet];
> scanner = [NSScanner scannerWithString: [self textInput]];
> numbersFound = [NSMutableString string];
>
> [scanner scanCharactersFromSet: numberChars intoString: &numbersFound];
>
> NSLog(@"The character set is %@\n", numberChars);
>
> if ([numbersFound length] == 0) {
>  NSLog(@"No numbers were found.\n");
>  [self setHasNumbers: NO];
> }
> else {
>  NSLog(@"The numbers found were %@\n", numbersFound);
>  [self setHasNumbers: YES];
> }
>}
>
>
>When I run the debugger, numbersFound gets assigned its memory space,
>but no numbers get scanned in. The value of the instance variable
>textInput was "one1two2three3", so it should have scanned in some
>numbers. My output from the logs is:
>
>2007-08-24 16:02:24.017 CheckForNumbers[1012] Init method went well.
>2007-08-24 16:02:24.019 CheckForNumbers[1012] The character set is
><NSBuiltinCharacterSet: 0x306c10>
>2007-08-24 16:02:24.019 CheckForNumbers[1012] No numbers were found.
>
>I must be missing something obvious about the way this scanner
>function works.

Well, reading the docs would help. The scanCharactersFromSet function "scans
the string as long as characters from a given character set are
encountered". The first character of your string is not from the given
character set, so that's the end of the scan.

Here is a version that works:

- (void) evaluateText {
    NSScanner* scanner = [NSScanner scannerWithString: self->inputText];
    NSCharacterSet* nums = [NSCharacterSet decimalDigitCharacterSet];
    NSString* s;
    self->hasNumber = NO;
    [scanner scanUpToCharactersFromSet:nums intoString:nil];
    while ([scanner scanCharactersFromSet:nums intoString:&s]) {
        NSLog(@"Found number: %@", s);
        self->hasNumber = YES;
        [scanner scanUpToCharactersFromSet:nums intoString:nil];
    }
}

m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Mail.app offline button
  • Next by Date: NSThread's and delegate methods
  • Previous by thread: Re: finding decimals in string
  • Next by thread: Dictionary
  • Index(es):
    • Date
    • Thread