在Java中,我想比较两个基本上是URL的字符串,因此,如果端点相同且域不同,则应返回true。 例如:https://www.example.org/dl/pi/1应该与http://1.1.1.1/dl/pi/1
有效匹配答案 0 :(得分:1)
您可以执行以下操作:
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));
如果URL
不正确,则会抛出MalformedURLException
。
可以找到Javadoc here | URL.