473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

String Manipulation

I need a piece of code that takes a string like this string1 =
"aaa/bbb/ccc/dd" and extracts a string containting the character after
the last "/"

So for this example the result would be "dd"

like this:
for i=0; string1.right(i) != '/'; i++

result = string1.mid(i, string1.length())

but in python.
Jul 18 '05 #1
6 7914
| I need a piece of code that takes a string like this
| string1 = "aaa/bbb/ccc/dd" and extracts a string containting
| the character after the last "/"
|
| So for this example the result would be "dd"
| ...

lamar_air ...

Here is one way ...

str_in = 'aaa/bbb/ccc/dd'

list_in = str_in.split( '/' )

last_element = list_in[ -1 ]

print last_element dd

--
Cousin Stanley
Human Being
Phoenix, Arizona
Jul 18 '05 #2
John Hunter wrote:
>>"lamar" == lamar air <la*******@hotmail.com> writes:

lamar> I need a piece of code that takes a string like this
lamar> string1 = "aaa/bbb/ccc/dd" and extracts a string
lamar> containting the character after the last "/"

One good way to do this is to split the string on the '/' character,
which creates a list of strings
>>> string1 = "aaa/bbb/ccc/dd"
>>> parts = string1.split('/')
>>> parts ['aaa', 'bbb', 'ccc', 'dd']

Now you just want to get the last element of this list; python allows
you to index with -1 to get the last element, -2 to get the second to
last, etc...
>>> parts[-1] 'dd'

JDH


While is is perfectly acceptable, you might want to consider another
solution if it is *path names* you are manipulating:
import os
os.path.split("aaa/bbb/ccc/dd") ('aaa/bbb/ccc', 'dd') os.path.split("aaa/bbb/ccc/dd")[1]

'dd'
because this will work correctly on other platforms too when the
path separator is not '/'.

--Irmen de JOng

Jul 18 '05 #3
I know a few people replied to this already, but here's one additional
possibility. If the data in string1 is a file path for your platform,
then you can use the os.path module.
import os.path
os.path.split("aaa/bbb/ccc/dd") ('aaa/bbb/cc', 'dd') os.path.splitext("filename.ext")
('filename', '.ext')

The other suggestions will of course work, but if you data is a file
path, then using the os.path module should be a more portable solution.

Brandon

"lamar_air" <la*******@hotmail.com> wrote in message
news:2c**************************@posting.google.c om... I need a piece of code that takes a string like this string1 =
"aaa/bbb/ccc/dd" and extracts a string containting the character after
the last "/"

So for this example the result would be "dd"

like this:
for i=0; string1.right(i) != '/'; i++

result = string1.mid(i, string1.length())

but in python.

Jul 18 '05 #4
On Tue, Jul 15, 2003 at 01:58:09PM -0700, Cousin Stanley wrote:
| I need a piece of code that takes a string like this
| string1 = "aaa/bbb/ccc/dd" and extracts a string containting
| the character after the last "/"
|
| So for this example the result would be "dd"
| ...

lamar_air ...

Here is one way ...

str_in = 'aaa/bbb/ccc/dd'

list_in = str_in.split( '/' )

last_element = list_in[ -1 ]

print last_element dd

--
Cousin Stanley
Human Being
Phoenix, Arizona


In Unix,
os.path.basename("aaa/bbb/ccc")

'ccc'

Inyeol

Jul 18 '05 #5
On 15 Jul 2003 13:23:09 -0700, la*******@hotmail.com (lamar_air) wrote:
I need a piece of code that takes a string like this string1 =
"aaa/bbb/ccc/dd" and extracts a string containting the character after
the last "/"

So for this example the result would be "dd"

like this:
for i=0; string1.right(i) != '/'; i++

result = string1.mid(i, string1.length())

but in python.


Others have posted the split() solutions.
You could also search backward in Python:
s = "aaa/bbb/ccc/dd"
s[s.rfind('/')+1:] 'dd' s='no_slashes'
s[s.rfind('/')+1:]

'no_slashes'

Regards,
Bengt Richter
Jul 18 '05 #6
On 15 Jul 2003 19:51:56 -0700, ee******@ee.iitm.ernet.in (Vinoo Vasudevan) wrote:
la*******@hotmail.com (lamar_air) wrote in message news:<2c**************************@posting.google. com>...
I need a piece of code that takes a string like this string1 =
"aaa/bbb/ccc/dd" and extracts a string containting the character after
the last "/"

So for this example the result would be "dd"

like this:
for i=0; string1.right(i) != '/'; i++

result = string1.mid(i, string1.length())

but in python.


How about this:
string1 = 'aaa/bbb/ccc/dd'
result = string1[string1.rfind('/')+1:]

Hope it's helpful,

Sorry Vinoo, for some reason your post did not show up for me before I posted
the same solution, even though your post is dated much before mine.
I guess it has to do with delays in news servers forwarding and such.

Regards,
Bengt Richter
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.