![](/img/trans.png)
[英]How to remove characters from a string after a certain point within a list?
[英]Remove string within certain characters
如果我有一個字符串如
string =“此文本為&此文本對於程序也是未知的&未知的”
如何刪除&字符中的所有內容?
使用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
你可以使用注冊表編輯器刪除它(我假設你也想&
消失):
import re
string = "This text is &This text is also unknown &unknown to the program"
sEdit = re.sub('&.*&', '', string)
print(sEdit)
或replace
+ find
:
print(string.replace(string[string.find('&'):string.find('&',string.find('&')+1)+1],''))
輸出:
This text is unknown to the program
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.