简体   繁体   中英

How to convert NSScanner value to string?

hi guys can you tell me how get the value from NSScanner in a string? infect 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM