从URL获取域名主机名的最快方法是什么?

从URL获取域名主机名的最快方法是什么?,第1张

从URL获取域名/主机名的最快方法是什么?

如果您想处理

https
等,我建议您执行以下 *** 作:

int slashslash = url.indexOf("//") + 2;domain = url.substring(slashslash, url.indexOf('/', slashslash));

请注意,这实际上包括了域名的

www
一部分(就像
URL.getHost()
这样做一样)。

编辑通过评论请求

以下是两种可能有用的方法:

public static String getHost(String url){    if(url == null || url.length() == 0)        return "";    int doubleslash = url.indexOf("//");    if(doubleslash == -1)        doubleslash = 0;    else        doubleslash += 2;    int end = url.indexOf('/', doubleslash);    end = end >= 0 ? end : url.length();    int port = url.indexOf(':', doubleslash);    end = (port > 0 && port < end) ? port : end;    return url.substring(doubleslash, end);}public static String getbaseDomain(String url) {    String host = getHost(url);    int startIndex = 0;    int nextIndex = host.indexOf('.');    int lastIndex = host.lastIndexOf('.');    while (nextIndex < lastIndex) {        startIndex = nextIndex + 1;        nextIndex = host.indexOf('.', startIndex);    }    if (startIndex > 0) {        return host.substring(startIndex);    } else {        return host;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5487026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存