title: Java 8 Get full URL path without last segment (with or without trailing slash)
body: <p>I have URLs as below and need to trim them as follows without the last segment. <em>There may or may not be a trailing slash.</em></p>
  
  <p><code>localhost:8080/myapp</code>  -> <code>localhost:8080/</code></p>
  
  <p><code>https://myapp-dev.myhost.com/app/</code> -> <code>https://myapp-dev.myhost.com/</code></p>
  
  <p><code>https://myapp-dev.myhost.com/app/app2</code> -> <code>https://myapp-dev.myhost.com/app/</code></p>
  
  <p>Of course I could try solutions like</p>
  
  <p><code>String[] tokens = uri.split("/"); // then concatenate previous ones...</code></p>
  
  <p>or</p>
  
  <pre><code>Path path = Paths.get(uri.getPath());
  String secondToLast = path.getName(path.getNameCount() - 2).toString();
  </code></pre>
  
  <p>But isn't there some more robust utility or method?</p>