Your code operates differently for "test blah,this". My code produces "test ,this" while yours produces "test this". Eliding multiple separators is perhaps more useful when sep=' ' but I used commas because they're easier to see.<div>
<br></div><div>An alternative design removes one separator either before or after a removed string (but not both). That would work better for an example like this:</div><div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">>>> remove('The Illuminati fnord are everywhere fnord.', 'fnord', sep=' ')</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">'The Illuminati are everywhere.'<br></font><div><br></div><div><div>Neither version of this may have sufficient utility to be added to standard library.</div>
<div><br></div><div>--- Bruce<br><a href="http://www.vroospeak.com" target="_blank">http://www.vroospeak.com</a><br><a href="http://j.mp/gruyere-security" target="_blank">http://j.mp/gruyere-security</a><br><br>
<br><br><div class="gmail_quote">On Thu, Oct 14, 2010 at 8:22 PM, Dj Gilcrease <span dir="ltr"><<a href="mailto:digitalxero@gmail.com">digitalxero@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Thu, Oct 14, 2010 at 7:45 PM, Bruce Leban <<a href="mailto:bruce@leapyear.org">bruce@leapyear.org</a>> wrote:<br>
> Here's a useful function along these lines, which ideally would be<br>
> string.remove():<br>
> def remove(s, sub, maxremove=None, sep=None):<br>
> """Removes instances of sub from the string.<br>
> Args:<br>
> s: The string to be modified.<br>
> sub: The substring to be removed.<br>
> maxremove: If specified, the maximum number of instances to be<br>
> removed (starting from the left). If omitted, removes all instances.<br>
> sep: Optionally, the separators to be removed. If the separator appears<br>
> on both sides of a removed substring, one of the separators is<br>
> removed.<br>
> >>> remove('test,blah,blah,blah,this', 'blah')<br>
> 'test,,,,this'<br>
> >>> remove('test,blah,blah,blah,this', 'blah', maxremove=2)<br>
> 'test,,,blah,this'<br>
> >>> remove('test,blah,blah,blah,this', 'blah', sep=',')<br>
> 'test,this'<br>
> >>> remove('test,blah,blah,blah,this', 'blah', maxremove=2, sep=',')<br>
> 'test,blah,this'<br>
> >>> remove('foo(1)blah(2)blah(3)bar', 'blah', 1)<br>
> 'foo(1)(2)blah(3)bar'<br>
> """<br>
<br>
</div>Could be written as<br>
<br>
def remove(string, sub, max_remove=-1, sep=None):<br>
if sep:<br>
sub = sub + sep<br>
return string.replace(sub, '', max_remove)<br>
<br>
t = 'test,blah,blah,blah,this'<br>
print(remove(t, 'blah'))<br>
print(remove(t, 'blah', 2))<br>
print(remove(t, 'blah', sep=','))<br>
print(remove(t, 'blah', 2, ','))<br>
print(remove('foo(1)blah(2)blah(3)bar', 'blah', 1))<br>
<font color="#888888"><br>
<br>
Dj Gilcrease<br>
____<br>
( | \ o () | o |`|<br>
| | /`\_/| | | ,__ ,_, ,_, __, , ,_,<br>
_| | | / | | |/ / / | |_/ / | / \_|_/<br>
(/\___/ |/ /(__,/ |_/|__/\___/ |_/|__/\__/|_/\,/ |__/<br>
/|<br>
\|<br>
</font></blockquote></div><br></div></div></div>