String Manipulation

L

lamar_air

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.
 
C

Cousin Stanley

| 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
 
I

Inyeol Lee

| 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

In Unix,
'ccc'

Inyeol
 
I

Irmen de Jong

John said:
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
['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...
'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
 
B

Brandon Beck

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.
('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
 
B

Bengt Richter

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
 
B

Bengt Richter

[email protected] (lamar_air) wrote in message news: said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top