Objective-C: Find numbers in string
Asked Answered
M

7

41

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?

Marivaux answered 11/1, 2011 at 22:32 Comment(1)
Have a look at a previously posted answer here.Landes
A
106

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.
Accrescent answered 11/1, 2011 at 22:54 Comment(1)
if i have multiple number at different place in string then it's return only first numberSmattering
R
57

Self contained solution:

+ (NSString *)extractNumberFromText:(NSString *)text
{
  NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  return [[text componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
}

Handles the following cases:

  • @"1234" → @"1234"
  • @"001234" → @"001234"
  • @"leading text get removed 001234" → @"001234"
  • @"001234 trailing text gets removed" → @"001234"
  • @"a0b0c1d2e3f4" → @"001234"

Hope this helps!

Rathskeller answered 4/12, 2014 at 23:21 Comment(2)
This is the best and decent answer in this post. It is better than accepted answer.Dihedron
Is it possible to strip the leading zeros as well?Drummer
T
5

You could use the NSRegularExpression class, available since iOS SDK 4.

Bellow a simple code to extract integer numbers ("\d+" regex pattern) :

- (NSArray*) getIntNumbersFromString: (NSString*) string {

  NSMutableArray* numberArray = [NSMutableArray new];

  NSString* regexPattern = @"\\d+";
  NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:regexPattern options:0 error:nil];

  NSArray* matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
  for( NSTextCheckingResult* match in matches) {
      NSString* strNumber = [string substringWithRange:match.range];
      [numberArray addObject:[NSNumber numberWithInt:strNumber.intValue]];
  }

  return numberArray; 
}
Testis answered 11/1, 2011 at 23:35 Comment(5)
Regular Expression is powerful. But in this simple case, I think @Rathskeller 's answer is the best.Dihedron
Regardless of my answer, I think using regex here might be an overkill and a bit more of a maintenance hazard; no one really understands regex and it's hard to debug.Rathskeller
@Rathskeller : I agree that it maybe is a little overkill, but I don't think it's a maintenance hazard. Actually it's really easy to add new functionality and a developer can reuse the pattern in any language that accepts regex.Testis
I add an example to make my answer more clear. It work for integer values. To expand to float values use this pattern: @"[\\d[.]]+"Testis
Very nice. Don't fear the regex.Lantha
F
3

Try this answer from Stack Overflow for a nice piece of C code that will do the trick:

for (int i=0; i<[str length]; i++) {
        if (isdigit([str characterAtIndex:i])) {
                [strippedString appendFormat:@"%c",[str characterAtIndex:i]];
        }
}
Florafloral answered 11/1, 2011 at 22:37 Comment(1)
I wouldn't recommend using a c-style for loop here; this could cause a significant lag on larger strings, especially if executed on the main thread.Rathskeller
H
1

By far the best solution! I think regexp would be better, but i kind of sux at it ;-) this filters ALL numbers and concats them together, making a new string. If you want to split multiple numbers change it a bit. And remember that when you use this inside a big loop it costs performance!

    NSString *str= @"bla bla bla #123 bla bla 789";
    NSMutableString *newStr = [[NSMutableString alloc] init];;
    int j = [str length];
    for (int i=0; i<j; i++) {       
        if ([str characterAtIndex:i] >=48 && [str characterAtIndex:i] <=59) {
            [newStr appendFormat:@"%c",[str characterAtIndex:i]];
        }               
    }

    NSLog(@"%@  as int:%i", newStr, [newStr intValue]);
Halfassed answered 11/1, 2011 at 22:46 Comment(0)
R
0

Swift extension for getting number from string

extension NSString {

func getNumFromString() -> String? {

    var numberString: NSString?
    let thisScanner = NSScanner(string: self as String)
    let numbers = NSCharacterSet(charactersInString: "0123456789")
    thisScanner.scanUpToCharactersFromSet(numbers, intoString: nil)
    thisScanner.scanCharactersFromSet(numbers, intoString: &numberString)
    return numberString as? String;
}
}
Ravioli answered 17/10, 2016 at 7:37 Comment(0)
A
-1

NSPredicate is the Cocoa class for parsing string using ICU regular expression.

Adamson answered 11/1, 2011 at 22:52 Comment(3)
NSPredicate regular expression matching doesn't work on iOS SDK.Math
Oh. Thanks, I didn't know that (spend most of my time in MacOS world).Adamson
NSPredicate has been part of the iOS SDK since iOS 3.0.Mania

© 2022 - 2024 — McMap. All rights reserved.