How to get the path of a URL

 URL url = new  URL("http://www.google.com/in/on");
 System.out.println(url.getPath());

Also See

  • Javadoc

I had performance doubts using the Java URL class for just extracting the path from an URL and thought that this is an overkill.

Therefore I wrote three methods, which all use a different way to extract the path from a given URL.

  1. 1st method uses the URL.getPath method from the Java URL class.
  2. 2nd method uses a regex I found in SO (I lost the source link, otherwise I'd give credits to the author right here).
  3. 3rd method uses a array-split and join for getting the same result.

All three methods are invoked 1000000 times for a given URL.

The result is:

#1 (getPathviaURL)   took:    860ms
#2 (getPathViaRegex) took:   3763ms
#3 (getPathViaSplit) took:   1365ms

Code - feel free to optimize it:

public static void main(String[] args) {


        String host = "http://stackoverflow.com/questions/5564998/how-to-get-the-path-of-a-url";

        long start1 = System.currentTimeMillis();
        int i = 0;
        while (i < 1000000) {
            getPathviaURL(host);
            i++;
        }
        long end1 = System.currentTimeMillis();

        System.out.println("#1 (getPathviaURL) took: " + (end1 - start1) + "ms");
        Pattern p = Pattern.compile("(?:([^:\\/?#]+):)?(?:\\/\\/([^\\/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");

        long start2 = System.currentTimeMillis();
        int i2 = 0;
        while (i2 < 1000000) {
            getPathViaRegex(host, p);
            i2++;
        }
        long end2 = System.currentTimeMillis();
        System.out.println("#2 (getPathViaRegex) Took: " + (end2 - start2) + "ms");

        long start3 = System.currentTimeMillis();
        int i3 = 0;
        while (i3 < 1000000) {
            getPathViaSplit(host);
            i3++;
        }
        long end3 = System.currentTimeMillis();
        System.out.println("#3 (getPathViaSplit) took: " + (end3 - start3) + "ms");



    }

    public static String getPathviaURL(String url) {
        String path = null;
        try {
            path = new URL(url).getPath();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return path;
    }

    public static String getPathViaRegex(String url, Pattern p) {
        Matcher m = p.matcher(url);

        if (m.find()) {
            return m.group(3);
        }
        return null;
    }

    public static String getPathViaSplit(String url) {
        String[] parts = url.split("/");

        parts = Arrays.copyOfRange(parts, 3, parts.length);
        String joined = "/" + StringUtils.join(parts, "/");

        return joined;
    }

You want something like this:

String path = new URL("http://www.costo.com/test1/test2").getPath();

Actually that'll give you /test1/test2. You'll just have to remove the first / to get what you want:

path = path.replaceFirst("/", "");

Now you'll have test1/test2 in path.


use URL.getPath() method of URL class.

Tags:

Java

Url

Uri