iOS获取/删除url中的参数
Posted Gerry_hu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS获取/删除url中的参数相关的知识,希望对你有一定的参考价值。
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:0 range:NSMakeRange(0, [url length])]; for (NSTextCheckingResult *match in matches) { NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]]; //分组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:0]; NSMutableString *lastStr = [strArray lastObject]; NSRange characterRange = [lastStr rangeOfString:@"&"]; if (characterRange.location !=NSNotFound) { NSArray *lastArray = [lastStr componentsSeparatedByString:@"&"]; NSMutableArray *mutArray = [NSMutableArray arrayWithArray:lastArray]; [mutArray removeObjectAtIndex:0]; NSString *modifiedStr = [mutArray componentsJoinedByString:@"&"]; finalStr = [[strArray objectAtIndex:0]stringByAppendingString:modifiedStr]; } else { //以‘?‘、‘&‘结尾 finalStr = [firstStr substringToIndex:[firstStr length] -1]; } return finalStr; }
以上是关于iOS获取/删除url中的参数的主要内容,如果未能解决你的问题,请参考以下文章
URLParser:很实用的URL参数解析器(JAVA代码中方便获取QueryString中的get参数)