2012-03-06 69 views
1

我需要您的幫助。我想從我的C#應用​​程序中的字符串拆分域名。 對此有任何想法。如何從字符串獲取域名

如:string strURL="http://stackoverflow.com/questions";

,我需要一個像 域名輸出:stackoverflow.com

+4

此問題已被問過1000次。在你的搜索能力上付出一點努力。 – 2012-03-06 08:03:31

+0

快速谷歌給了這個許多可行的答案。 – Corbin 2012-03-06 08:03:41

+0

嗨juergen,請參考我的鏈接找到最好的解決方案 – 2012-03-06 09:26:21

回答

6

這應該工作。

新的URI( 「http://stackoverflow.com/questions」).DnsSafeHost

0

您可以使用正則表達式做....

  string domainName = string.Empty; 
      string strURL="http://stackoverflow.com/questions"; 
      Regex rg = new Regex("://(?<host>([a-z\\d][-a-z\\d]*[a-z\\d]\\.)*[a-z][-a-z\\d]+[a-z])"); 
      if (rg.IsMatch(strURL)) 
      { 
       domainName = rg.Match(strURL).Result("${host}"); 
      } 

則domainName給域名稱.....

+0

謝謝阿卡什,請檢查此網址,如www.google.co.in/somthing ...像子域也。 – 2012-03-06 09:36:16