NSString *codeStr = @"http://qzone.qq.com/?username=aaaa&password=bbbb";
如何截取到"username"?
方法一:根据特殊字符截取:componentsSeparatedByString方法
NSArray *array1 = [codeStr componentsSeparatedByString:@"?"];
NSString *str1 = array1[1];//username=aaaa&password=bbbb
NSArray *array2 = [str1 componentsSeparatedByString:@"&"];
NSString *str2 = array2[0];//username=aaaa
NSArray *array3 = [str2 componentsSeparatedByString:@"="];
NSString *str3 = array3[1];//aaaa
username = str3;
方法二:NSURLComponents属性判断:componentsWithString方法
参考资料:http://masilotti.com/nsurlqueryitems/ (免翻译~~)
官方文档:https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLComponents_class/index.html#//apple_ref/occ/instp/NSURLComponents/query
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:inviteCodeStr];
for (NSURLQueryItem *item in urlComponents.queryItems) {
if ([item.name isEqualToString:@"username"]) {
username = item.value;
return;
}
}
其实也就是个key-value。