【发布时间】:2013-07-29 09:27:05
【问题描述】:
有时我会删除或替换一个长字符串的子字符串。因此,我将确定一个开始模式和一个结束模式,这将确定子字符串的起点和终点:
long_string = "lorem ipsum..white chevy..blah,blah...lot of text..beer bottle....and so to the end"
removed_substr_start = "white chevy"
removed_substr_end = "beer bott"
# this is pseudo method down
STRresult = long_string.replace( [from]removed_substr_start [to]removed_substr_end, "")
【问题讨论】:
-
您是否考虑过使用正则表达式?
-
很久以前,但不记得一些开始 - 结束指向的方法。
-
我认为您过于专注于计算起点和终点,而不是您真正想要实现的目标。
-
是的,但它似乎很有用,我想知道如果没有内置函数,我是否应该构建自己的函数。
-
有,而且是正则表达式,看我的回答。 (编辑:现在所有其他答案)