string.upto() and string.from()

I

ikshefem

I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :

# canonical examples
"1234456789".upto("45") '1234'
"123456dd987".from('d')
'd987'

# if not found, return whole string
"hello, world !".upto("#") "hello, world !"
u"hello, world !".from("#")
u"hello, world !"
"123456dd987".from('d',2) # second integer argument
'987'

# It can be negative, too
'192.168.179.131'.upto(".",-1) "192.168.179"
"192.168.179.131".from('.',-1)
"131"

# useful example
bigstring.upto("\n")
"first line of bigstring"

(I admit I am only using upto, but ...)

Nothing very complicated to make with find and rfind, but wouldn't this
be handy to have it ready in the common string method ?
 
D

Duncan Booth

I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :
Nothing very complicated to make with find and rfind, but wouldn't this
be handy to have it ready in the common string method ?

Something similar to this was discussed at length on the development list
last year. I don't think the discussion reached any concrete decision, but
the proposal was to add partition and rpartition methods to strings:

As described by Time Delaney:

partition() splits a string into 3 parts - the bit before the
first occurrance of the separator, the separator, and the bit
after the separator. If the separator isn't in the string at
all then the entire string is returned as "the bit before" and
the returned separator and bit after are empty strings.

See http://thread.gmane.org/gmane.comp.python.devel/70739
 
F

Fredrik Lundh

I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :

# canonical examples
'd987'

# if not found, return whole string
u"hello, world !"

'987'

# It can be negative, too
"131"

# useful example
"first line of bigstring"

(I admit I am only using upto, but ...)

Nothing very complicated to make with find and rfind, but wouldn't this
be handy to have it ready in the common string method ?

somewhat related:

http://www.python.org/dev/summary/2005-08-16_2005-08-31.html#str-find

</F>
 
K

Kent Johnson

FWIW this is pretty easy to do with str.split() and rsplit():
"1234456789".split("45", 1)[0]
'1234'
"1234456789".rsplit("45", 1)[-1]
'6789'


"hello, world !".split("#", 1)[0]
'hello, world !'

"hello, world !".rsplit("#", 1)[-1]
'hello, world !'

Kent
 
I

ikshefem

Sure, you're right I forgot about rsplit !
I guess the negative indexes & al could be done with

sep.join(xyz.split(sep)[:index])


Thanks !
 
K

Kent Johnson

Sure, you're right I forgot about rsplit !
I guess the negative indexes & al could be done with

sep.join(xyz.split(sep)[:index])

For index=-1 use
xyz.rsplit(sep, 1)[0]

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,131
Latest member
KatlynC08

Latest Threads

Top