【问题标题】:Compare path of url strings. Ignoring domain names比较 url 字符串的路径。忽略域名
【发布时间】:2018-06-27 04:50:13
【问题描述】:

在Java中,我想比较两个基本上是url的字符串,所以如果端点相同而域不同,它应该返回true。 例如:https://www.example.org/dl/pi/1 应该是与 http://1.1.1.1/dl/pi/1 的有效匹配

【问题讨论】:

  • 一个是http,另一个是https?
  • 解析两者并仅比较感兴趣的部分?请展示您到目前为止所做的尝试。
  • 是的,我也想在比较时忽略 http 或 https 等,但它应该确保它是有效的 url 格式。
  • edit您的问题包括其他信息以及您的代码
  • 请不要使用正则表达式stackoverflow.com/questions/5564998/…

标签: java uri


【解决方案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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 2019-08-03
    • 2010-09-26
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多