URL编码:
-
(NSString *)URLEncodedString
{
NSString *unencodedString = self;
NSString *encodedString = (NSString )
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)unencodedString, NULL, (CFStringRef)@"!'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));return encodedString;
}
URL解码:
- (NSString *)URLDecodedString
{
NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
return [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
建议这两个方法放在NSString的分类中,并且声明为实例方法,这样可以直接用NSString的对象去调用。
网友评论