1.获取URL中的某个参数:
- (NSString *)getParameter:(NSString *)parameter urlStr:(NSString *)url { NSError *error; if (!url) { return@""; } NSString *regTags = [[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",parameter]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *matches = [regex matchesInString:url options: range:NSMakeRange(, [url length])]; for (NSTextCheckingResult *match in matches) { NSString *tagValue = [url substringWithRange:[match rangeAtIndex:]]; //分组2所对应的串 return tagValue; } return@""; }
2.删除URL中的某个参数:
- (NSString *)deleteParameter:(NSString *)parameter WithOriginUrl:(NSString *)originUrl { NSString *finalStr = [NSString string]; NSMutableString * mutStr = [NSMutableString stringWithString:originUrl]; NSArray *strArray = [mutStr componentsSeparatedByString:parameter]; NSMutableString *firstStr = [strArray objectAtIndex:]; NSMutableString *lastStr = [strArray lastObject]; NSRange characterRange = [lastStr rangeOfString:@"&"]; if (characterRange.location !=NSNotFound) { NSArray *lastArray = [lastStr componentsSeparatedByString:@"&"]; NSMutableArray *mutArray = [NSMutableArray arrayWithArray:lastArray]; [mutArray removeObjectAtIndex:]; NSString *modifiedStr = [mutArray componentsJoinedByString:@"&"]; finalStr = [[strArray objectAtIndex:]stringByAppendingString:modifiedStr]; } else { //以'?'、'&'结尾
finalStr = [firstStr substringToIndex:[firstStr length] -]; } return finalStr; }