字符串'abcdfdbcg',输出去掉含‘bc’后的字符
# coding:utf-8 ''' 字符串'abcdfdbcg',输出去掉含‘bc’后的字符 ''' # -*- 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: 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]) # enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标 if __name__ == '__main__': str1 = 'abcxybcg' str2 = 'bc' print(rm_str(str1, str2))