给定以下URL:
foo.bar.car.com.au
我需要提取foo.bar
。
我遇到了以下代码:
private static string GetSubDomain(Uri url)
{
if (url.HostNameType == UriHostNameType.Dns)
{
string host = url.Host;
if (host.Split('.').Length > 2)
{
int lastIndex = host.LastIndexOf(".");
int index = host.LastIndexOf(".", lastIndex - 1);
return host.Substring(0, index);
}
}
return null;
}
这给了我像 foo.bar.car
这样的东西。我想要的是 foo.bar。我应该只使用 split 并取 0 和 1 吗?
但是可能会有 wwww。
有没有简单的方法可以做到这一点?
foo.bar
是foo.bar.car.com.au
中的子域名? - Mike Perrenoud