我有以下网址,我想从中提取id参数:
assets-library://asset/asset.JPG?id = 123456789 & ext = JPG
我想知道是否有更好的方法来计算id
的价值而不是:
NSString *id = [[[[def.url.query componentsSeparatedByString:@"id="]
lastObject]
componentsSeparatedByString:@"&"]
objectAtIndex:0];
答案 0 :(得分:1)
在你的情况下,沿着这些方向的东西可能会起作用(有些简化,但有效):
NSString *string = @"assets-library://asset/asset.JPG?id=123456789&ext=JPG";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"id=([^&]+)" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSRange range = [match rangeAtIndex:1];
NSString *idSting = [string substringWithRange:range];
NSLog(@"%@", idSting);