题目:请实现一个函数,将一个字符串中的空格替换成 “%20”。例如,当字符串为 We Are Happy. 则经过替换之后的字符串为 We%20Are%20Happy。
Python代码如下:
class Solution:
def replaceSpace(self, s):
# method 1
return s.replace(' ','%20')
def replaceSpace2(self, s):
# method 2
news = ''
for item in s:
if item != ' ':
news += item
else:
news += "%20"
return news