find nth occurance in a string?
Alex Martelli
aleax at aleax.it
Wed Jan 16 03:54:29 EST 2002
"Greg Goodman" <chironsw at swbell.net> wrote in message
news:3C44E3DC.1E91D934 at swbell.net...
>
> > pieces = theString.split(searchString, n)
> > if len(pieces) != n+1: return -1
> > return len(TheString)-len(pieces[n])+len(searchString)
>
> That's a neat trick; I like it. Coming from a 'C' background, my string
> processing is usually low-level and brute force. Snippets like this are
> helping me to learn Pythonic idiom as well as to think about string
> manipulation differently.
>
> Just to show I'm paying attention, I think the last line should be:
>
> return len(TheString)-(len(pieces[n])+len(searchString))
Ooops, yes, it should -- or, the + in the return statement should
be changed into a -. Now let's see if I can pull off a claim
that I had deliberately inserted this buglet to
[a] check who was paying attention and/or
[b] subliminally advertise for test-first coding...
Alex
More information about the Python-list
mailing list