Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

extracting numbers from NSString



Hi,

I am trying to extract some numbers from an NSString and store them in an array (as an NSString, not as an NSNumber).

The original string has a format such as:

test (1.2.3 4.5.6)

The array should look like {123, 456}, so only numbers are left, without the periods.


I tried all kind of combinations of NSScanner and NSCharacterSets, but haven't been succesful yet. One of the attemps I tried is:


skipSet = [[NSCharacterSet lowercaseLetterCharacterSet] mutableCopy];
[skipSet formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];

aScanner = [NSScanner scannerWithString: testString];
[ aScanner setCharactersToBeSkipped: skipSet]; // skip all letters and brackets

while (![aScanner isAtEnd])
{
if ([aScanner scanUpToCharactersFromSet: [NSCharacterSet whitespaceAndNewlineCharacterSet] intoString: &number])
{
[temp addObject: number];
}
}

[skipSet release];




Anyone has a suggestion of a way to do this?


thanks,

- Koen.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.