Extracting number from NSString

I have an NSString which when logged gives me an answer like this one:

Response: oauth_token_secret=6h8hblp42jfowfy&oauth_token=9tmqsojggieln6z

The two numbers change every single time.

Is there a way to extract the two numbers and create two strings with one of each??

Like:

NSString *key = @"9tmqsojggieln6z";

//copy the string in a new string variable
NSMutableString *auth_token = [NSMutableString stringWithString:response];

NSRange match = [auth_token rangeOfString: @"&oauth_token="];
[auth_token deleteCharactersInRange: NSMakeRange(0, match.location+13)];
//auth_token will now have the auth token string

NSMutableString *auth_token_secret = [NSMutableString stringWithString:response];
NSRange range1 = [auth_token_secret rangeOfString:[NSString stringWithFormat:@"&oauth_token=%@", auth_token]];
[auth_token_secret deleteCharactersInRange:range1];
NSRange range2 = [auth_token_secret rangeOfString:@"oauth_token_secret="];
[auth_token_secret deleteCharactersInRange: range2];

//auth_token_secret will have the required secret string.

I had the same problem. As response I get the ids of objects sometimes as string sometimes as numbers. Then I wrote a category for NSDictionary which has the following method:

- (NSString *)stringFromStringOrNumberForKey:(NSString *)key
{
id secret = [self objectForKey:key];
if ([secret isKindOfClass:[NSNumber class]]) {
    NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];

    secret = [numberFormatter stringFromNumber:secret];
}

return secret;

}


我会尝试以下方法:

NSString *_response = @"oauth_token_secret=6h8hblp42jfowfy&oauth_token=9tmqsojggieln6z";
NSMutableDictionary *_dictionary = [[NSMutableDictionary alloc] init];
NSArray *_parameters = [_response componentsSeparatedByString:@"&"];
for (NSString *_oneParameter in _parameters) {
    NSArray *_keyAndValue = [_oneParameter componentsSeparatedByString:@"="];
    [_dictionary setValue:[_keyAndValue lastObject] forKey:[_keyAndValue objectAtIndex:0]];
}

// reading the values
NSLog(@"token_secret : %@", [_dictionary valueForKey:@"oauth_token_secret"]);
NSLog(@"token : %@", [_dictionary valueForKey:@"oauth_token"]);
链接地址: http://www.djcxy.com/p/85232.html

上一篇: Python,Unicode和Windows控制台

下一篇: 从NSString中提取数字