字符串相关

字符串替换

  • 问题:请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字 符串为We%20Are%20Happy。

  • 解决:

    #方法一:
    class Solution:
        # s 源字符串
        def replaceSpace(self, s):
            # write code here
            return s.replace(" ","%20")
    #方法二:
    class Solution:
        # s 源字符串
        def replaceSpace(self, s):
            # write code here
            list = s.split(" ")
            res = "%20".join(list)
            return res
    
posted @ 2020-07-22 22:17  guguda  阅读(71)  评论(0)    收藏  举报