Home | Articles|Namespace|Interview Questions|Tools|Jobs|Projects|Community
Asp.net Tutorials

»Dotnet Ads
»Message Boards
Message Boards
Dotnet Books

»Member Details
Register
Login
LogOut
Submit Code
Submit Jobs
Submit Projects

»Competition
Community
Winners
Prizes
Write For Us
Members

»Other Resources
Links
Dotnet Resources

c# extracting domain from a url

I was scouring the web for a function which can extract domain from a url and came across this code

/*
** Method 1 (using the build-in Uri-object)
*/
public static string ExtractDomainName(string Url)
{


int pos = Url.indexOf("://");
if (pos == -1 || pos > 5)
Url = "http://" + Url;

return new Uri(Url).Host;

}

/*
** Method 2 (using string modifiers)
*/
public static string ExtractDomainName(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 ExtractDomainName(string Url)
{
return System.Text.RegularExpressions.Regex.Replace(
Url,
@"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$",
"$2"
);
}

originally posted at
http://www.jonasjohn.de/snippets/csharp/extract-domain-name-from-url.htm

Have a Question and dont know the answer post it below and get answers in minutes

Due to spam this feature is disabled
To get answers fast , make sure you enter a detailed subject for example: "DataGrid issues need answer" not "DataGrid"

Subject:

Catjegory Name:

Message:



© 2008 dotnetwatch.com -- Privacy policy