find nth occurance in a string?

Greg Goodman chironsw at swbell.net
Tue Jan 15 21:15:20 EST 2002


>         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))


Thanks,

Greg Goodman



More information about the Python-list mailing list