iPhone 中文URL编码(encode)解码(decode)

URL 加码 & 解码:

NSString* string1 = @"https://www.cloudsafe.com/文件夹";

    

NSString* string1 = @"https://www.cloudsafe.com/文件夹";

    

NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

NSString* string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

NSString* string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

输出结果: string5 & 6 重复编码encode

 

string1:https://www.cloudsafe.com/文件夹

 

string2:https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

string3:https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9

 

string4:https://www.cloudsafe.com/文件夹

string5:https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

 

string6:https://www.cloudsafe.com/文件夹

string7:https://www.cloudsafe.com/文件夹


Python中的字符串编码解码可以通过encode()和decode()方法来实现。这两个方法都是字符串对象的方法,用于将字符串从一种编码格式转换为另一种编码格式。 encode()方法可以将字符串编码为指定的编码格式,其语法为: ```python string.encode(encoding=编码格式, errors=错误处理) ``` 其中,encoding参数是必须的,用于指定要使用的编码格式,errors参数是可选的,用于指定错误处理方式。如果不指定errors参数,则默认使用strict模式,即如果存在无法编码的字符,则会抛出UnicodeError异常。 例如,将一个字符串编码为UTF-8格式: ```python string = "Hello World" encoded_string = string.encode(encoding="utf-8") print(encoded_string) ``` 输出结果为:b'Hello World'。其中,b表示这是一个字节串对象,而不是字符串对象。 decode()方法可以将字节串解码为指定的编码格式,其语法为: ```python bytes.decode(encoding=编码格式, errors=错误处理) ``` 其中,encoding参数是必须的,用于指定要使用的编码格式,errors参数是可选的,用于指定错误处理方式。如果不指定errors参数,则默认使用strict模式,即如果存在无法解码的字节,则会抛出UnicodeError异常。 例如,将一个UTF-8格式的字节串解码为字符串: ```python bytes_string = b'Hello World' decoded_string = bytes_string.decode(encoding="utf-8") print(decoded_string) ``` 输出结果为:Hello World。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值