Zopi
Zopi

Reputation: 235

How to use NSScanner?

I've just read Apple documentation for NSScanner. I'm trying to get the integer of this string:

@"user logged (3 attempts)"

I can't find any example, how to scan within parentheses. Any ideas?

Here's the code:

NSString *logString = @"user logged (3 attempts)";
NSScanner *aScanner = [NSScanner scannerWithString:logString];

[aScanner scanInteger:anInteger];

NSLog(@"Attempts: %i", anInteger);

Upvotes: 19

Views: 36715

Answers (5)

Gal Blank
Gal Blank

Reputation: 2090

here is the reg-ex usage

NSString *logString = @"user logged (3 attempts)";
NSString * digits = [logString stringByMatching:@"([+\\-]?[0-9]+)" capture:1];

Upvotes: 1

NSResponder
NSResponder

Reputation: 16857

Ziltoid's solution works, but it's more code than you need.

I wouldn't bother instantiating an NSScanner for the given situation. NSCharacterSet and NSString give you all you need:

    NSString *logString = @"user logged (3 attempts)";
    NSString *digits = [logString stringByTrimmingCharactersInSet:
         [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
    NSLog(@"Attempts: %i", [digits intValue]);

or in Swift:

    let logString = "user logged (3 attempts)"
    let nonDigits = NSCharacterSet.decimalDigitCharacterSet().invertedSet
    let digits : NSString = logString.stringByTrimmingCharactersInSet(nonDigits)
    NSLog("Attempts: %i", digits.intValue)

Upvotes: 53

user155959
user155959

Reputation:

NSScanner is a linear scanner. You have to scan through the stuff you don't want to get to what you do want.

You could do [aScanner scanUpToCharactersInSet:[NSCharacterSet decimalDigitCharacterSet] intoString:NULL] to jump past everything up to the number character. Then you do [aScanner scanInteger:&anInteger] to scan the character into an integer.

Upvotes: 5

zaph
zaph

Reputation: 112857

Here is a simple solution using NSScanner (yes, @NSResponder has a really neat solution!):

NSString *logString = @"user logged (3 attempts)";
NSString *numberString;
NSScanner *scanner = [NSScanner scannerWithString:logString];
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&numberString];
NSLog(@"Attempts: %i", [numberString intValue]);

NSLog output:

Attempts: 3

Upvotes: 7

Ziltoid
Ziltoid

Reputation: 482

`Here is what I do to get certain values out of a string

First I have this method defined

- (NSString *)getDataBetweenFromString:(NSString *)data leftString:(NSString *)leftData rightString:(NSString *)rightData leftOffset:(NSInteger)leftPos; 
{         
    NSInteger left, right;         
    NSString *foundData;
    NSScanner *scanner=[NSScanner scannerWithString:data];                  
    [scanner scanUpToString:leftData intoString: nil];         
    left = [scanner scanLocation];         
    [scanner setScanLocation:left + leftPos];         
    [scanner scanUpToString:rightData intoString: nil];         
    right = [scanner scanLocation] + 1;         
    left += leftPos;         
    foundData = [data substringWithRange: NSMakeRange(left, (right - left) - 1)];         return foundData; 
}

Then call it.

foundData = [self getDataBetweenFromString:data leftString:@"user logged (" rightString:@"attempts)" leftOffset:13];

leftOffset is the number of characters for the left string

Could be an easier cleaner way but that was my solution.

Upvotes: 17

Related Questions