两个网址进行比较的方法

  1. using System;
  2. using System.Collections.Generic;
  3. namespace SiteCompare
  4. {
  5.     
  6.     
  7.     class SiteCompare
  8.     {
  9.         private string ConvertStr(ref string cons)
  10.         {
  11.             char[] arr;
  12.             arr = cons.ToCharArray();
  13.             Array.Reverse(arr);
  14.             return new string(arr);
  15.         }
  16.         private string TrimStr(ref string trs)
  17.         {
  18.             int found;
  19.             string tempstr;
  20.             found = trs.IndexOf("/");
  21.             tempstr = trs.Substring(found + 2).Trim();  
  22.             return ConvertStr(ref tempstr);
  23.         }
  24.         public bool CompareSite(string a, string b)
  25.         {
  26.             int alength = TrimStr(ref a).Split('.').Length;
  27.             int blength = TrimStr(ref b).Split('.').Length;
  28.             int minlength = alength > blength ? blength : alength;
  29.             int maxlength = alength > blength ? alength : blength;
  30.             for(int i=0; i<minlength; i++)
  31.             {
  32.                 if(TrimStr(ref a).Split('.')[i].Equals(TrimStr(ref b).Split('.')[i], StringComparison.OrdinalIgnoreCase))
  33.                 {
  34.                     Console.WriteLine(ConvertStr(ref TrimStr(ref a).Split('.')[i])+","+ConvertStr(ref TrimStr(ref b).Split('.')[i]));
  35.                     continue;
  36.                 }
  37.                 else
  38.                 {
  39.                     Console.WriteLine(ConvertStr(ref TrimStr(ref a).Split('.')[i])+","+ConvertStr(ref TrimStr(ref b).Split('.')[i]));
  40.                     return false;
  41.                 }
  42.             }
  43.             if(minlength!=maxlength)
  44.             {
  45.                 string fsite = TrimStr(ref a).Split('.')[maxlength-1];
  46.                 if(fsite != "www")
  47.                 {
  48.                     return false;
  49.                 }
  50.             }
  51.             return true;
  52.         }
  53.     }
  54.     class MainClass
  55.     {
  56.         public static void Main(string[] args)
  57.         {
  58.             string s1 = "http://www.baidu.com";
  59.             string s2 = "http://baidu.com";
  60.             SiteCompare sc = new SiteCompare();
  61.             Console.WriteLine(s1+","+s2);
  62.             Console.WriteLine(sc.CompareSite(s1, s2));
  63.             Console.ReadLine();
  64.         }
  65.     }
  66. }
大家可以试试,看看有没有不合理的地方留言给我,谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值