![](/img/trans.png)
[英]How can I remove special characters from a string without using any external library?
[英]how to remove sub string from string without using any functions
我有文字
abcdblobefgblobhijk
How to remove the substring blob from the string without using any in built functions which provide 1 line solution like String.replace etc. In other words program should get the string as input and without using any inbuilt function should output the string without substring "blob ”。
我嘗試盡可能“手動”地進行操作,沒有任何快捷方式或python技巧,
我希望這對您有幫助:
text = "abcdblobefgblobhijk"
sub = "blob"
new_text =''
i=0
last_i = 0
while i < len(text):
if text[i:i+len(sub)] == sub:
new_text += text[last_i:i]
last_i=i+len(sub)
i=i+len(sub)
else:
i+=1
new_text += text[last_i:i]
print(new_text)
def remove(string, substr):
str_length = len(string)
substr_length = len(substr)
for i in range(str_length):
if string[i] == substr[0]:
end = i + substr_length
temp = string[i:end]
if substr == temp:
return string[:i] + string[end:]
return string
print(remove("Dhaval Dhanesha","sha"))
Output:達瓦爾丹
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.