栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从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;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494905.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号