You can determine the host name of a Uri object with Uri.Host. That would be the host at http://asdf.site.csharp-console-examples.com/index.htm
If you only want to have csharp-console-examples.com, you can use this snippet.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Program { static void Main(string[] args) { string urlString = "http://asdf.site.csharp-console-examples.com/index.htm"; Console.WriteLine(GetDomainNameOfUrlString(urlString)); Console.Read(); } private static string GetDomainNameOfUrlString(string urlString) { var host = new Uri(urlString).Host; return host.Substring(host.LastIndexOf('.', host.LastIndexOf('.') - 1) + 1); } } |
Doesn’t work with co.uk etc