ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#获取顶级域名的方法

C#获取顶级域名的方法

原创 Linux操作系统 作者:fangqm 时间:2010-04-02 13:18:20 0 删除 编辑
xxx.com  xxx.xxx.com.cn    最终得到xxx.com   xxx.com.cn
网上没有找到符合我要求的正则表达式
 
我用最笨的方法来实现
编程思路
得到标准的网址,截取两个或三个.两边的字符串
1。分割 .  两边的字符串
2。取到最后一个点,两边字符串
3。与com.cn net.cn org.cn比较
4。如果相同,就取两个点两边的字符为域名
控制台应用程序
static void Main(string[] args)
        {
            Console.Write("请输入一个标准网址:");
            string url = Console.ReadLine();
            //分割.两边的字符串
            char[] a = { '.'};
            //由于目前域名后缀中,最长的只有这个三.com.cn .net.cn .org.cn
            //那么顶级域名后缀,先取最长的两个来与.com.cn .net.cn .org.cn比较
            string[] temp = url.Split(a);
            string domain = temp[temp.Length - 2] + "." + temp[temp.Length-1];
            if (domain == "com.cn" || domain == "net.cn" || domain == "org.cn")
            {
                domain = temp[temp.Length - 3] + "." + temp[temp.Length - 2] + "." + temp[temp.Length-1];
            }
           //如果和以上三个相同,那么域名就再向左截一个点
            Console.Write("域名为:"+domain);
           
            Console.ReadLine();
        }
_____________________________________________________
   ///
        /// 分析指定网址的域名
        ///

        ///
        /// 返回一个域名
        public string GetUrlDomainName(string url)
        {

            //正则表达式匹配结果
            Match m = reg.Match(url);
            //返回匹配结果值
            url = m.Groups["domain"].Value;

            //把标准网址.两边的字符分割出来
            char[] a = { '.' };
            string[] temp = url.ToLower().Split(a);
            //取最后一个点两边的字符串
            string domain = temp[temp.Length - 2] + "." + temp[temp.Length - 1];
            //目前域名两个点的只有这三个 com.cn   net.cn  org.cn
            if (domain == "com.cn" || domain == "net.cn" || domain == "org.cn")
            {
                domain = temp[temp.Length - 3] + "." + temp[temp.Length - 2] + "." + temp[temp.Length - 1];
            }
            return domain;
        }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-631091/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: WebBrowser控件简介
请登录后发表评论 登录
全部评论
创业项目 https://u.rscode.cn

注册时间:2010-03-24

  • 博文量
    297
  • 访问量
    784643