Objective-C: Find numbers in string

Nic Hubbard picture Nic Hubbard · Jan 11, 2011 · Viewed 36.5k times · Source

I have a string that contains words as well as a number. How can I extract that number from the string?

NSString *str = @"This is my string. #1234";

I would like to be able to strip out 1234 as an int. The string will have different numbers and words each time I search it.

Ideas?

Answer

martin clayton picture martin clayton · Jan 11, 2011

Here's an NSScanner based solution:

// Input
NSString *originalString = @"This is my string. #1234";

// Intermediate
NSString *numberString;

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

// Throw away characters before the first number.
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];

// Collect numbers.
[scanner scanCharactersFromSet:numbers intoString:&numberString];

// Result.
int number = [numberString integerValue];

(Some of the many) assumptions made here:

  • Number digits are 0-9, no sign, no decimal point, no thousand separators, etc. You could add sign characters to the NSCharacterSet if needed.
  • There are no digits elsewhere in the string, or if there are they are after the number you want to extract.
  • The number won't overflow int.

Alternatively you could scan direct to the int:

[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
int number;
[scanner scanInt:&number];

If the # marks the start of the number in the string, you could find it by means of:

[scanner scanUpToString:@"#" intoString:NULL];
[scanner setScanLocation:[scanner scanLocation] + 1];
// Now scan for int as before.