Skip to content

Instantly share code, notes, and snippets.

@tmngtk
Created September 4, 2009 14:46
Show Gist options
  • Save tmngtk/180927 to your computer and use it in GitHub Desktop.
Save tmngtk/180927 to your computer and use it in GitHub Desktop.
#
# my_replace("abcdefg", "bc", "ef")
# returns "ag"
#
# my_replace("abcdefg", "bc", "ef", "012")
# returns "a012g"
#
def my_replace(s, start, end, rep = ''):
buf = []
while s:
a = s.split(start, 1)
if len(a) == 1:
buf.append(a[0])
break
b = a[1].split(end, 1)
if len(b) == 1:
buf.append(b[0])
break
buf+= [buf.append(a[0]), rep, b[1]]
s = b[1]
return ''.join(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment