Talk:Getting portions of a file path

Is there a reason why these functions weren't written using lastIndexOf, like so: function getFilename(path) { var pos = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); return path.substr(pos + 1); }

function getExtension(path) { var pos = path.lastIndexOf('.'); return (pos < 0) ? "" : path.substr(pos + 1); }

I think these functions are easier to understand, and will very possibly execute slightly quicker, if there are any optimizations in JavaScript's lastIndexOf. Unless lastIndexOf is proven to be slow...

--Slthytove 19:12, 20 Oct 2005 (UTC)

Yeah, your way is better ;)
I wrote these in a hurry and didn't think of using lastIndexOf which is odd because use it all the time normally. By all means change the page to display your versions of the functions - they're cleaner and like you say, easier to understand.

-William

For getFilenameWithoutExtension, do you consider files like .profile to be just extensions? If so getFilenameWithoutExtension works as expected. --Crccheck 01:16, 24 Oct 2005 (UTC)
 * I guess so, then. ;-) --Slthytove 12:50, 25 Oct 2005 (UTC)