473,486 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1586
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
10271
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
1
5298
by: Craig G | last post by:
i have the little jscript which i use for returning a value from a popupModal. but i want to extend this so that i cant split the string into different values in future the string will return a...
3
485
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
5
4592
by: Sakharam Phapale | last post by:
Hi All, How to declare the following statement in following structure. szPname As String * MAXPNAMELEN Public Structure MIXERCAPS public wMid As Integer public ...
4
1579
by: Diffident | last post by:
Hello All, I have a string of around 150 characters. string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd........."; //(upto 150 characters) I want to split the above string into multiple...
6
1240
by: Diffident | last post by:
Dear Bruce, Can you please explain me how compiler would be able to optimize "+" for the below code? The reason why I did not want to use string concatenation was that I wanted it to be a...
3
3237
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
3
52498
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
3
3312
by: maheswaran | last post by:
HI all, I have file management system, in this system have the option to upload the file into server. this application provide the possibility to upload four files in a time, that is have four...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.