title: JS: Most optimized way to remove a filename from a path in a string?
body: <p>I have strings formatted as follows:
  <br><code>path/to/a/filename.txt</code></p>
  
  <p>Now I'd like to do some string manipulation which allows me to very efficiently remove the "filename.txt" part from this code. In other words, I want my string to become this:
  <br><code>path/to/a/</code></p>
  
  <p>What's the most efficient way to do this? Currently I'm splitting the string and reconnecting the seperate elements except for the last one, but I get the feeling this is a really, REALLY inefficient way to do it. Here's my current, inefficient code:</p>
  
  <pre><code>res.getPath = function(file)
  {
    var elem = file.split("/");
    var str = "";
    for (var i = 0; i &lt; elem.length-1; i++)
      str += elem[i] + "/";
    return str;
  }
  </code></pre>