views:

5894

answers:

5

Hi,

I've just read Apple's docu of NSScanner. I'm trying to get the integer of that 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);

Regrads,

Ruby

+1  A: 

`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;                          
    [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.

Ziltoid
Just a note to those copying the above code: you should use NULL, not nil, in the "intoString:" argument. Because the value is passed by reference, I believe there will be a leak otherwise.
Kalle
+3  A: 

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.

Preston
+8  A: 

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]);
NSResponder
What is "source" in your example?Did you mean "logString" instead?
Donna
Oops.. Corrected.
NSResponder
`invertedSet` nice! I wasn't aware of that method. I can't remember how I solved this issue before, but it surely wasn't as elegant as this.
kubi
A: 

that was awesome...very close to wat i m looking for....

just having one problem,,

my server output is "OK: message-ID XXX110088358XXX
"

i m using above function to extract a number from this output. but the function seems to return 2.which is in there size=2

thats the servers's output OK: message-ID XXX110088358XXX

that i cant change..i need to get the what is in between XXX ....XXXX

any way to do this.

nice job by the way guys

regards shishir

shishir.bobby
A: 

how to scan the last 2 numbers of this string 200,8,1.27,103.84

i want just 1.27 and 103.84 in 2 separate strings

ram
You should really go ask a separate question and get it answered there. But for your example, you can use the NSString to split your items into an array (one for each number separated by comma). `NSArray *chunks = [string componentsSeparatedByString: @","]` Then just pull out the last two parts of the array.
christophercotton