推荐编程平台
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
分析
题目要求替换空格,使用Python可以轻松的实现这个效果,使用Python的内置函数replace即可
replace()
str.replace(old, new[, max])
old-- 将被替换的子字符串。new-- 新字符串,用于替换old子字符串。max-- 可选字符串, 替换不超过 max 次
代码
# -*- coding:utf-8 -*-
class Solution:
# s 源字符串
def replaceSpace(self, s):
# write code here
return s.replace(' ','%20')
本文由 Bearever 创作,除注明转载/出处外,均为本站原创,转载前请务必署名
最后编辑时间为: 2017-10-10 16:30 星期二