Extract domain name from URL
Shows three different methods how to extract the domain name of a given web adress.
/* ** Method 1 (using the build-in Uri-object) */ public static string ExtractDomainNameFromURL_Method1(string Url) { if (!Url.Contains("://")) Url = "http://" + Url; return new Uri(Url).Host; } /* ** Method 2 (using string modifiers) */ public static string ExtractDomainNameFromURL_Method2(string Url) { if (Url.Contains(@"://")) Url = Url.Split(new string[] { "://" }, 2, StringSplitOptions.None)[1]; return Url.Split('/')[0]; } /* ** Method 3 (using regular expressions -> slowest) */ public static string ExtractDomainNameFromURL_Method3(string Url) { return System.Text.RegularExpressions.Regex.Replace( Url, @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", "$2" ); }
Author:
Jonas John
License:
Public domain
Language:
C#
Created:
10/30/2007
Updated:
10/30/2007
Tags:
string functions, http
// Some example urls: string[] Urls = new string[] { "http://www.jonasjohn.de/snippets/csharp/", "www.jonasjohn.de/snippets/csharp/", "http://www.jonasjohn.de/", "ftp://www.jonasjohn.de/", "www.jonasjohn.de/", "https://subdomain.abc.def.jonasjohn.de/test.htm" }; // Test all urls with all different methods: foreach (string Url in Urls){ Console.WriteLine("Method 1: {0}", ExtractDomainNameFromURL_Method1(Url)); Console.WriteLine("Method 2: {0}", ExtractDomainNameFromURL_Method2(Url)); Console.WriteLine("Method 3: {0}", ExtractDomainNameFromURL_Method3(Url)); }
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
Request.Url.Host
I am having a little trouble though, and I can't see why. I need to extract the domain name from a URL in an ETL program for server logs, and while I can do this succesfully with any of those methods, even the fastest is slowing my program down by about 20x. I can't really see why these would be such expensive operations, but the evidence is in front of me - if I replace the operation with a string to test, the application is MUCH quicker. Are these methods really that expensive?
Thanks
static string GetDomainName(string url)
{
string domain = url.Split(new string[] { "://" }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
domain = domain.Split(new char[] { '/' })[0];
string[] sectons = domain.Split(new char[] { '.' });
if (sectons.Length >= 3)
{
domain = string.Join(".", sectons, sectons.Length - 2, 2);
}
return domain;
}
{
string domain = url.Split(new string[] { "://" }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
domain = domain.Split(new char[] { '/' })[0];
string[] sectons = domain.Split(new char[] { '.' });
if (sectons.Length == 3)
{
domain = string.Join(".", sectons, 1, 2);
}
return domain;
}
www.yahoo.com/red.aspx?a=http://www.cool.com
then http isnt pasted before www.yahoo.com
int pos = Url.indexOf("://");
if (pos == -1 || pos > 5)
Url = "http://" + Url;
return new Uri(Url).Host;