»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 urlI 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
|