471,119 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

string.upto() and string.from()

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 ?

Mar 22 '06 #1
5 1505
ik******@gmail.com wrote:
I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :
<snip>
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
Mar 22 '06 #2
ik******@gmail.com wrote:
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 ?


somewhat related:

http://www.python.org/dev/summary/20....html#str-find

</F>

Mar 22 '06 #3
Fredrik Lundh wrote:
ik******@gmail.com wrote:

I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :
FWIW this is pretty easy to do with str.split() and rsplit():
# canonical examples
"1234456789".upto("45")


'1234'
"1234456789".split("45", 1)[0]
'1234'
"123456dd987".from('d')


'd987'
"1234456789".rsplit("45", 1)[-1]
'6789'

# if not found, return whole string
"hello, world !".upto("#")


"hello, world !"

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


u"hello, world !"


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

Kent
Mar 22 '06 #4
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 !

Mar 22 '06 #5
ik******@gmail.com wrote:
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
Mar 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hal Vaughan | last post: by
1 post views Thread by Craig G | last post: by
3 posts views Thread by Dominique Vandensteen | last post: by
5 posts views Thread by Sakharam Phapale | last post: by
4 posts views Thread by Diffident | last post: by
6 posts views Thread by Diffident | last post: by
3 posts views Thread by maheswaran | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.