user3650408 user3650408 - 5 months ago 11
Java Question

URL parse without String split

Java has cool URL parser

import java.net.*;
import java.io.*;

public class ParseURL {
public static void main(String[] args) throws Exception {

URL aURL = new URL("http://example.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING");

System.out.println("path = " + aURL.getPath());
}
}


Here is the output displayed by the program:

path = /docs/books/tutorial/index.html


I would like to take only this part :
docs/books/tutorial
(or
/docs/books/tutorial/
) Guessing don`t use string split, I am looking for other better resolution for this task.

Thank you in advance

Answer
String path = "/docs/books/tutorial/index.html";
path = path.substring(1, path.lastIndexOf("/"));

gives docs/books/tutorial