• 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: extracting numbers from NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: extracting numbers from NSString


  • Subject: Re: extracting numbers from NSString
  • From: Julio Cesar Silva dos Santos <email@hidden>
  • Date: Tue, 31 Jan 2006 21:43:33 -0200

If your strings are separated by a single space you could use the following code:

NSArray * result = [yourString componentsSeparatedByString:@" "];

Then you could use NSScanner to skip the period in every object of result.

Julio Cesar Santos
email@hidden
eMac 1GHz ComboDrive
640MB RAM
Linux User #359973


On 31/01/2006, at 19:02, Koen van der Drift wrote:

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:
This email sent to email@hidden


References: 
 >extracting numbers from NSString (From: Koen van der Drift <email@hidden>)

  • Prev by Date: extracting numbers from NSString
  • Next by Date: Re: A quick memory (release) question
  • Previous by thread: extracting numbers from NSString
  • Next by thread: NSTask, environment and ... X11
  • Index(es):
    • Date
    • Thread