簡體   English   中英

刪除某些字符內的字符串

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM