Get the Filename part of a Path from a String
Login or Register to Bookmark this snippet
This extends every string with another method. Usage:
var str = 'c:\windows\system32\wmp.dll';
var filename = str.getFileName();
String.prototype.getFileName = function() {
var pos = this.lastIndexOf('\\');
return this.substring(pos+1);
}
Added by JC on 3rd January, 2008
-
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.
You must be registered and logged in to post a comment.
Login here to post a comment