Python - 用python实现split函数

# pattern支持字符或者字符串
def my_split(string, pattern):
    ret = []
    len_pattern = len(pattern)
    while True:
        index = string.find(pattern)
        if index == -1:
            ret.append(string)
            return ret
        else:
            ret.append(string[:index])
            string = string[index+len_pattern:]
            

if __name__=="__main__":
    print(my_split("abcded", "cd"))
    print(my_split("abcded", "d"))
posted @ 2019-05-27 20:39  Rocin  阅读(673)  评论(0编辑  收藏  举报