How to convert NSScanner value to string?
hi guys can you tell me how get the value from NSScanner in a string? infe开发者_开发问答ct i have to extract int value from a string i am extracting it using scanner now my value is in scanner. now how to get that value?
NSString *string = @" rate 50%";
int n;
NSScanner * scanner = [[NSScanner alloc] initWithString:string];
[scanner scanInt:&n];
now where is my value and how to use?
I wouldn't bother with NSScanner in your instance.
Give this a go.
NSString *string = @" rate 50%";
NSString *digits = [string stringByTrimmingCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"Value: %i", [digits intValue]);
I agree with @Lee that you needn't bother with NSScanner in your example. But in your example, the int n
is set to 50. That's why you pass the address of n
to scanInt
–so scanInt
can change its value.
精彩评论