C# 根据链接获取一级域名和当前域名
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpStudy { class Program { static void Main(string[] args) { string url = "http://www.xinhuanet.com/2018-08/02/c_1123215278.htm"; //string url = "http://news.ifeng.com/a/20180803/59600750_0.shtml"; Uri uri = new Uri(url); string topDomain = GetBaseDomain(uri.Host); Console.WriteLine("当前域名:" + uri.Host); Console.WriteLine("一级域名:" + topDomain); Console.ReadKey(); } private static string GetBaseDomain(string host) { HashSet<string> HashSet = new HashSet<string>(".com|.co|.info|.net|.org|.me|.mobi|.us|.biz|.xxx|.ca|.co.jp|.com.cn|.net.cn|.org.cn|.mx|.tv|.ws|.ag|.com.ag|.net.ag|.org.ag|.am|.asia|.at|.be|.com.br|.net.br|.bz|.com.bz|.net.bz|.cc|.com.co|.net.co|.nom.co|.de|.es|.com.es|.nom.es|.org.es|.eu|.fm|.fr|.gs|.in|.co.in|.firm.in|.gen.in|.ind.in|.net.in|.org.in|.it|.jobs|.jp|.ms|.com.mx|.nl|.nu|.co.nz|.net.nz|.org.nz|.se|.tc|.tk|.tw|.com.tw|.idv.tw|.org.tw|.hk|.co.uk|.me.uk|.org.uk|.vg".Split('|')); string[] hs = host.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); if (hs.Length > 2) { //传入的host地址至少有三段 int p2 = host.LastIndexOf('.'); //最后一次“.”出现的位置 int p1 = host.Substring(0, p2).LastIndexOf('.');//倒数第二个“.”出现的位置 string s1 = host.Substring(p1); if (!HashSet.Contains(s1)) return s1.TrimStart('.'); //域名后缀为两段(有用“.”分隔) if (hs.Length > 3) return host.Substring(host.Substring(0, p1).LastIndexOf('.')); else return host.TrimStart('.'); } else if (hs.Length == 2) { return host.TrimStart('.'); } else { return string.Empty; } } } }
后续会陆续更新其他资料,喜欢请关注哦!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee