水平不高,查找库的时候一直找不到这个功能,于是自己花老鼻子劲写了个。记录一下,以后有更好的方法回头看看。写的时候发现用python的for i in n循环在我的方法实现中并不是很方便,i的值不能在循环中被修改?while倒是可以实现这点,也算是小小收获。
def delete_sign(string, sign):
r = ''
i = 0
j = 0
while i < len(string):
match_count = ""
while j < len(sign):
# print("string[" + str(i) + "]:", string[i], " ", "sign[" + str(j) + "]", sign[j])
if string[i] == sign[j]:
match_count = match_count + string[i]
# print(match_count)
if match_count == sign:
# print("a complete match!")
j = 0
break
i = i + 1
else:
# print("match_count is " + match_count + " string[" + str(i) + "] is ", string[i])
if match_count == "":
r = r + string[i]
# print("r is ", r)
else:
r = r + match_count + string[i]
j = 0
break
j = j + 1
i = i + 1
return r