Sometimes the odd helper class is useful. This one might even be a decent candidate for some .NET 3.5 extension methods. These URL utilities are quite self explanatory and by no means are a complete set of URL helper methods that would be useful, but who knows, they might have something you're looking for.
using System;
using System.Collections.Generic;
using System.Text;
namespace HelperCode
{
internal static class UrlUtils
{
internal static string GetTldFromUrl(string url)
{
string tld = UrlUtils.GetHostFromUrl(url);
if (tld != null && tld.Contains('.'))
{
string[] parts = tld.Split('.');
if (parts.Length > 0)
{
tld = parts[parts.Length - 1];
}
}
return tld;
}
internal static string GetHostFromUrl(string url)
{
string retval = null;
try
{
Uri uri = new Uri(url);
retval = uri.Host.ToLower();
}
catch
{
retval = null;
}
return retval;
}
internal static string GetSchemeFromUrl(string url)
{
string retval = null;
try
{
Uri uri = new Uri(url);
retval = uri.Scheme.ToLower();
}
catch
{
retval = null;
}
return retval;
}
}
}
If you have a better way to do it, please, by all means, let us know. There are no doubt better ways. :)
Whether it be in code or business deals, complexity kills. If you're an architect and you love the elegance of endless inheritance where everything is a descendant of MyCoolRootObject or an venture capitalist trying to tie off every risk with carefully structured language that leaves a founder in the lurch and you in the driver's seat of the getaway car, you are the enemy of success. If you flout the team's style guild and name your class members with freaky names and patterns only you can recognize, you are an enemy of success. If you're a framework developer and you believe you have to add every possible toy feature in the universe to your framework, you are an enemy of success.
And enemies of success lose! Lovers of complexity may win a battle here or there, but the ash heap of history is full of them. Consider any number of technologies that have become so overbloated and difficult to work with that developers and architects look for simpler solutions. Examine the many thousands of failed startups killed by pencil pushing pinheads with no other agenda than to make the deal difficult in hopes of making it perfect, only to kill the deal with a stulted obsession with detail and gaining the advantage in every paragraph.
Simplicity is the key to success.