Compare path of url strings. Ignoring domain names

418 views Asked by At

In Java, I want to compare two strings which are basically the urls, so if the endpoints are same and the domain is different it should return true. For example: https://www.example.org/dl/pi/1 should be a valid match with http://1.1.1.1/dl/pi/1

1

There are 1 answers

0
Shanu Gupta On BEST ANSWER

You could do something like this:

String url1 = new URL("http://1.1.1.1/dl/pi/1").getPath(); // returns /dl/pi/1
String url2 = new URL("https://www.example.org/dl/pi/1").getPath(); // returns /dl/pi/1

System.out.println(url1.equals(url2));

If the URL is not correct it'll throw MalformedURLException.

Javadoc can be found here | URL.