开发者

How to convert NSScanner value to string?

开发者 https://www.devze.com 2023-03-29 06:24 出处:网络
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 h

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号