Twinfriends
Twinfriends

Reputation: 1987

Remove first part of a string that haven't fixed length

I ran into some problems with my code and I don't know how to fix it. So my problem:

On a View of my application, there is a filepath displayed like this:

/resume/attachment/12/yaml_error_complete.yml

But I only want the filename as Output, means:

yaml_error_complete.yml

How can i achieve this? I tried with several options like string.slice! etc, but it doesn't work, since the number after "attachment" is increased by 1 for every single upload. At beginning I thought about to simply remove 2 chars, don't matter what they are. But then i ran into another problem that happens when the 100 file is uploaded. In this case i would have to remove 3 chars instead of 2, and i'm again at the beginning of my problem.

May someone of you can help me?

Thanks a lot!

Upvotes: 1

Views: 59

Answers (3)

peter
peter

Reputation: 42182

In addition to the sulution with split('/') you could do the following

File.basename("/resume/attachment/12/yaml_error_complete.yml")

Upvotes: 2

Bala Karthik
Bala Karthik

Reputation: 1413

I assume that you need to retrieve the file of a path.

for ex if your file name is "/resume/attachment/12/yaml_error_complete.yml"

Then try this one

"/resume/attachment/12/yaml_error_complete.yml".split('/').last

Upvotes: 3

Oleksandr Holubenko
Oleksandr Holubenko

Reputation: 4440

If I understand your question right, then, maybe it should help you:

a = "/resume/attachment/12/yaml_error_complete.yml"
a.split('/').last
#=> "yaml_error_complete.yml"

Upvotes: 3

Related Questions