NSString 以UTF8f进行url编码 和 解码

关于URL加上特定的的编解码,用到了下面两个接口。

编码:
其实就是把NSString 先用特定的encoding 方式编码,在把他URL 编码。
主要作用就是把一个string编码成特定编码方式的URL可用string。

下面这个函数名字翻译~:string 通过添加%转义符用编码方式:
stringByAddingPercentEsc apesUsingEncoding:
Returns a representation  of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

- (NSString *)stringByAddingPercentEsc apesUsingEncoding :(NSStringEncoding)encoding
Parameters
encoding
The encoding to use for the returned string.

Return Value
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character

解码:
对应的解码。
stringByReplacingPercent EscapesUsingEncoding:
Returns a new string made by replacing in the receiver all percent escapes with the matching characters as determined by a given encoding.

- (NSString *)stringByReplacingPercent EscapesUsingEncoding:(NSStringEncoding)encoding
Parameters
encoding
The encoding to use for the returned string.

Return Value
A new string made by replacing in the receiver all percent escapes with the matching characters as determined by the given encoding encoding. Returns nil if the transformation is not possible, for example, the percent escapes give a byte sequence not legal in encoding.

注释:PercentEscapes %转义符
===========================================我的使用例子==================================

       NSString *st1 = [@" Cocoa:好的APIs(1)- <wbr>UTF8<or <wbr>others> <wbr>url <wbr>编码和解码,可以中文 "
                                         stringByReplacingPercent EscapesUsingEncoding:NSUTF8StringEncoding ];
       NSLog(@"%@",st1);
     
       NSString *st2 = [@"银泰" stringByAddingPercentEsc apesUsingEncoding:NSUTF8StringEncoding];
       NSLog(@"%@",st2);

输出结果:
Cocoa:好的APIs(1)- <wbr>UTF8<or <wbr>others> <wbr>url <wbr>编码和解码,可以中文



之后也在NSURL中测试过,可以使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值