如何解析/拆分和打印URL?

问题描述

有人可以帮助解析网址吗?

我有如下网址-

实际-https://www.facebook.com/group.php/gid=12345
期待-facebook /group.php/gid=12345

实际-https://www.Test.this.domain/testPath/
期待-https wwwtestthisdomain testpath

解决方法

您可以像下面这样使用java.net.URI

    URI uri = URI.create("https://www.facebook.com/group.php/gid=12345");
    System.out.println(
        uri.getPath() // prints: /group.php/gid=12345
    );
    System.out.println(
        uri.getHost() // www.facebook.com
    );

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...