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 ? 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 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>
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
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 ! 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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 ...
|
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...
| |
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...
|
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 ).
...
|
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,...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |