Get the Filename part of a Path from a String

Description Javascript

This extends every string with another method. Usage:
    var str = 'c:\windows\system32\wmp.dll';
    var filename = str.getFileName();

The Code Download

  1. String.prototype.getFileName = function() {
  2.     var pos = this.lastIndexOf('\\');
  3.     return this.substring(pos+1);
  4. }

Credits

Added by JC on 3rd January 2008

Comments

JC 3rd January 2008, 10:27 pm

Damn, it stripped my slashes. There are meant to be two of the slashes inside the lastIndexOf function. But you get the idea. Only works for Windows file paths, *nix systems would use only one of the forward slashes instead.

JC 3rd January 2008, 10:42 pm

Ok slashes resolved. The code is displayed exactly as it's meant to now.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment