如果我有一个字符串,例如
string =“此文本为&此文本也是程序未知&未知”
如何删除&字符中的所有内容?
答案 0 :(得分:2)
使用find的简单方法:
s = "This text is &This text is also unknown &unknown to the program"
start = s.find('&')
end = s.find('&', start+1)
result = s[:start] + s[end+1:]
print(result)
输出
This text is unknown to the program
答案 1 :(得分:2)
您可以使用regedit将其删除(我想您还希望&
消失):
import re
string = "This text is &This text is also unknown &unknown to the program"
sEdit = re.sub('&.*&', '', string)
print(sEdit)
答案 2 :(得分:0)
或replace
+ find
:
print(string.replace(string[string.find('&'):string.find('&',string.find('&')+1)+1],''))
输出:
This text is unknown to the program