去除字符串中的特定字符串

字符串'abcdfdbcg',输出去掉含‘bc’后的字符  # str.replace("old","new")
# -*- coding:utf-8 -*-

def rm_str(str1, str2):
    i, j = 0, len(str1)
    s, e = 0, len(str2)

    tmp_list = []
    while i < j and s < e:
        while i < j and str1[i] != str2[s]:
            i += 1

        t = i
        while i < j and s < e and str1[i] == str2[s]:
            i += 1
            s += 1

        if s == e:
            print("t的值为:"+str(t))
            tmp_list.extend(list(range(t, i)))
            s = 0
        else:
            s = 0
            i = t+1

    return ''.join([s for n, s in enumerate(str1) if n not in tmp_list])

if __name__ == '__main__':
    str1 = 'abcxybcg'
    str2 = 'bc'
    print(rm_str(str1, str2))

 

posted @ 2021-11-05 20:45  keep2021  阅读(435)  评论(0)    收藏  举报