• 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: Andrew Merenbach <email@hidden>
  • Date: Fri, 24 Aug 2007 13:23:43 -0700

Oh, yes, and in order to *find* the digits, use - rangeOfCharactersFromSet:options:range: with a while loop, incrementing the beginning of the search range each time that you find another character.

You could also do use a for-loop with -[NSString characterAtIndex:] and [NSCharacterSet characterIsMember:], but you must be sure that your string doesn't contain composed character sequences in order for it to work properly.

Cheers,
	Andrew

On Aug 24, 2007, at 1:04 PM, Daniel Child wrote:

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. I am also wondering if this is the normal approach to find out whether a string contains certain characters of interest. Or is there a class I don't know about?

Much thanks.

Daniel
_______________________________________________

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

_______________________________________________

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


References: 
 >finding decimals in string (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: finding decimals in string
  • Next by Date: Re: CGContextSetStyle console log error?
  • Previous by thread: Re: finding decimals in string
  • Next by thread: Re: finding decimals in string
  • Index(es):
    • Date
    • Thread