-1

I have a list of IDs that I wish to extract, specifically for ID the intermediate values.

For example, the 1st ID is : garden/trade.FX.fwd/nyk12523adn

I wish to remove the values 'garden/', and '/nky12523adn', perhaps using string.replace()...

How might I do that efficiently? The starting logic would be to

  1. string.find('/'), to get the location
  2. then remove the prefix for the first '/', and suffix for the 2nd '/'

Is there an efficient way to do this?

Kiann
  • 531
  • 1
  • 6
  • 20
  • 1
    Possible duplicate of [Get a string after a specific substring](https://stackoverflow.com/questions/12572362/get-a-string-after-a-specific-substring) – sansa Nov 29 '19 at 11:00

1 Answers1

1

you can also use .split() method

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Syntax

string.split(separator, maxsplit)
id = "garden/trade.FX.fwd/nyk12523adn"
print (id.split('/')[1])

output:

trade.FX.fwd

if you want to use .find() function:

id = "garden/trade.FX.fwd/nyk12523adn"

idx_start = id.find('/') # find index of first '/'
idx_end = id.find('/',idx_start + 1) # find index of second '/'

print (id[idx_start+1:idx_end]) # use list slicing

output :

trade.FX.fwd
ncica
  • 7,015
  • 1
  • 15
  • 37