Re: nicer way to remove prefix of a string if it exists

2010-07-14 Thread Paul McGuire
On Jul 13, 6:49 pm, News123 wrote: > I wondered about a potentially nicer way of removing a prefix of a > string if it exists. > Here is an iterator solution: from itertools import izip def trim_prefix(prefix, s): i1,i2 = iter(prefix),iter(s) if all(c1==c2 for c1,c2 in izip(i1,i2)):

Re: nicer way to remove prefix of a string if it exists

2010-07-14 Thread Tim Chase
On 07/13/2010 09:22 PM, Shashwat Anand wrote: You could write: rsl = f[len(prefix):] if f.startswith(prefix) else f Or you can just do split and join, "".join(f.split(prefix, 1)) will do. This suggestion breaks if the prefix occurs within the string rather than at the beginning: f

Re: nicer way to remove prefix of a string if it exists

2010-07-13 Thread Shashwat Anand
On Wed, Jul 14, 2010 at 5:42 AM, MRAB wrote: > News123 wrote: > >> I wondered about a potentially nicer way of removing a prefix of a >> string if it exists. >> >> >> Here what I tried so far: >> >> >> def rm1(prefix,txt): >>if txt.startswith(prefix): >>return txt[len(prefix):] >>

Re: nicer way to remove prefix of a string if it exists

2010-07-13 Thread MRAB
News123 wrote: I wondered about a potentially nicer way of removing a prefix of a string if it exists. Here what I tried so far: def rm1(prefix,txt): if txt.startswith(prefix): return txt[len(prefix):] return txt for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]:

Re: nicer way to remove prefix of a string if it exists

2010-07-13 Thread Juan Andres Knebel
Hi, for rm1 I think something like this could work: def rm1(prefix,txt): regular_expresion = re.compile(r''+prefix+'(.*)') return re.match(regular_expresion,txt).group(1) On Tue, Jul 13, 2010 at 8:49 PM, News123 wrote: > I wondered about a potentially nicer way of removing a prefix of a >

nicer way to remove prefix of a string if it exists

2010-07-13 Thread News123
I wondered about a potentially nicer way of removing a prefix of a string if it exists. Here what I tried so far: def rm1(prefix,txt): if txt.startswith(prefix): return txt[len(prefix):] return txt for f in [ 'file:///home/me/data.txt' , '/home/me/data.txt' ]: # method 1 i