URL化 替换空格
url 替换 空格
2023-09-11 14:15:15 时间
URL化 替换空格
编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。
示例 1:
- 输入:"Mr John Smith ", 13
- 输出:"Mr%20John%20Smith"
示例 2:
- 输入:" ", 5
- 输出:"%20%20%20%20%20"
示例代码1:
# 方法一:调用库函数
class Solution(object):
def replaceSpaces(self, S, length):
"""
:type S: str
:type length: int
:rtype: str
"""
return S[:length].replace(' ', '%20')
a = Solution()
# b = a.replaceSpaces("Mr John Smith ", 13)
# b = a.replaceSpaces("Mr John Smith ", 14)
b = a.replaceSpaces(" ", 5)
print(b)
示例代码2:
# 方法二:单纯耿直的循环替换
class Solution(object):
def replaceSpaces(self, S, length):
"""
:type S: str
:type length: int
:rtype: str
"""
URL = []
for char in S[:length]:
if char == ' ':
URL.append('%20')
else:
URL.append(char)
return ''.join(URL)
a = Solution()
# b = a.replaceSpaces("Mr John Smith ", 13)
# b = a.replaceSpaces("Mr John Smith ", 14)
b = a.replaceSpaces(" ", 5)
print(b)
运行效果:
相关文章
- 为什么返回的数据前面有callback? ashx/json.ashx?的后面加 callback=? 起什么作用 js url?callback=xxx xxx的介绍 ajax 跨域请求时url参数添加callback=?会实现跨域问题
- Python 获取重定向url
- iOS开发--URL中汉字出现乱码
- 【Web】URL解析
- nginx服务器设置url的优雅链接
- UrlUtils工具类,Java URL工具类,Java URL链接工具类
- Python 网络教程大全之 05 如何获取url下载文件的大小?
- 微信测试号:config:invalid url domain
- 请问wordpress,使用markdown生成页面如何跳转url?
- 不刷新网页修改url链接(history.pushState和history.replaceState新增、替换历史记录用法介绍)、popstate玩转浏览器历史记录介绍、如何触发popstate事件、popstate事件的应用场景、history.state查看当前页面数据介绍
- JAVA 获取 URL 指定参数的值
- js获取url中指定参数的值(含带hash)
- 路过秋天版博客 V2.0 正式版发布 增加后台管理系统[支持多语言、多用户、多数据库、目录级URL]
- nginx根据URL地址、user_agent设备类型、文件扩展名 实现动静分离代理转发
- layui jquery ajax,url,type,async,dataType,data