Snippets v2

Languages
My Snippets

Get the Filename part of a Path from a String Login or Register to Bookmark this snippet

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 Contact JC

Added by JC on 3rd January, 2008

Comments
3rd January, 2008 7:27 am JC
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.
3rd January, 2008 7:42 am JC
Ok slashes resolved. The code is displayed exactly as it's meant to now.
Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment