//urlEncode编码 + (NSString *)urlEncodeStr:(NSString *)input { NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "; NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet]; NSString *upSign = [input stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters]; return upSign; } //urlEncode解码 + (NSString *)decoderUrlEncodeStr: (NSString *) input { NSMutableString *outputStr = [NSMutableString stringWithString:input]; [outputStr replaceOccurrencesOfString:@"+" withString:@"" options:NSLiteralSearch range:NSMakeRange(0,[outputStr length])]; return [outputStr stringByRemovingPercentEncoding]; } 转成python代码
时间: 2023-06-18 10:03:15 浏览: 52
Python中有自带的urllib库可以进行URL编码和解码,代码如下:
```python
import urllib.parse
def urlEncodeStr(input):
charactersToEscape = "!?@#$^&%*+,:;='\"`<>()[]{}/\\| "
allowedCharacters = urllib.parse.quote(charactersToEscape, safe='')
upSign = urllib.parse.quote(input, safe=allowedCharacters)
return upSign
def decoderUrlEncodeStr(input):
outputStr = urllib.parse.unquote(input.replace("+", ""))
return outputStr
```
相关问题
java+urlencode编码转换
在Java中进行URL编码转换时,空格默认会被转换为"+"号。如果你需要将空格转换为"%20",可以使用以下方法:
1. 使用`URLEncoder`类进行编码,并使用`replace`方法将"+"替换为"%20":
```java
String encodedStr = URLEncoder.encode(content, "UTF-8").replace("+", "%20");
```
2. 使用Apache Commons Codec库中的`URLCodec`类进行编码,并设置`encode`方法的第二个参数为`"UTF-8"`,将"+"替换为"%20":
```java
URLCodec codec = new URLCodec("UTF-8");
String encodedStr = codec.encode(content).replace("+", "%20");
```
这样,你就可以将空格正确地转换为"%20"。
urlencode编码与解码
urlencode是一种将特殊字符转换成%xx形式的编码方式。其中,xx表示字符的ASCII码的16进制表示。urlencode编码通常用于URL中的参数值,以避免特殊字符对URL的解析造成影响。
例如,将字符串"hello, world!"进行urlencode编码后,得到的结果为"hello%2C%20world%21"。
在Python中,可以使用urllib库中的quote和unquote函数来进行urlencode编码和解码。如下所示:
```python
import urllib
# urlencode编码
encoded_str = urllib.parse.quote("hello, world!")
print(encoded_str)
# 输出:hello%2C%20world%21
# urlencode解码
decoded_str = urllib.parse.unquote("hello%2C%20world%21")
print(decoded_str)
# 输出:hello, world!
```
在PHP中,可以使用urlencode和urldecode函数来进行urlencode编码和解码。如下所示:
```php
// urlencode编码
$encoded_str = urlencode("hello, world!");
echo $encoded_str;
// 输出:hello%2C+world%21
// urlencode解码
$decoded_str = urldecode("hello%2C+world%21");
echo $decoded_str;
// 输出:hello, world!
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)