兩個網址進行比較的方法

  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. }
大家可以試試,看看有沒有不合理的地方留言給我,謝謝!
發佈了15 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章