1

Right now, I get the last part of an URL to exclude the filename like this:

attachmentFileName = attachmentLink.split("/");
attachmentFileName = attachmentFileName[attachmentFileName.length-1];

it feels, as if I could do that in one statement only, but I can't figure out how. Not a big deal, but i think it can be done more elegantly.

3
  • 1
    attachmentFileName = attachmentLink.split("/")[attachmentFileName.length-1]; Mind. Blown. Commented Jan 18, 2016 at 10:25
  • Are you on on NodeJS? Node has utilities for handling paths and URLs which are more robust than splitting on the slash. Commented Jan 18, 2016 at 10:25
  • Glubus that's really nice :) write it as an answer so i can accept it Commented Jan 18, 2016 at 10:33

2 Answers 2

3

You can use Array.prototype.pop() method to retrieve last element of your splitted array:

attachmentFileName = attachmentLink.split("/").pop();
Sign up to request clarification or add additional context in comments.

Comments

1

Please give a try to below code

attachmentFileName = attachmentLink.substring(attachmentLink.lastIndexOf("/")+1,attachmentLink.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.