Leif160519的blog Leif160519的blog

——————

目录
URL encode编码和解码
/    

URL encode编码和解码

URL encode 用于将字符串以 URL 编码,具体代码如下:

+(NSString *)encodeString:(NSString *)unencodeString
{
    NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)unencodeString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
    return unencodeString;
}

-(NSString *)decodeString:(NSString *)encodedString
{
    NSString * decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)encodedString, CFSTR(""),CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return encodedString;
}

当然也可以不写方法体,直接复制方法内部的代码,把相关变量替换即可。

附: URL在线解析: [ UrlEncode编码/UrlDecode解码 - 站长工具 ]


“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题URL encode编码和解码
作  者Leif160519
出  处https://github.icu/articles/2019/08/23/1566541937866.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!