By now, many of you have seen where a URL contains text similar to the page's title. For example, the URL may look like http://www.domain.com/this-is-my-best-article.aspx instead of http://www.domain.com/bestarticle.aspx. Text converted from a regular string
, that can appear within a URL this way is called a "slug."
Not only is a slug a little more human-readable, but it can also help indicate to search engines like Google and Bing what keywords are important to your page.
There's no built-in .NET function to convert a string
to a slug. I found a few examples on the web but didn't find any I really liked. So I decided to roll my own in C#. Listing 1 shows my ConvertTextToUrl()
method. It takes any string
and makes it safe to include as part of a URL.
Initially, I started by looking for an official, comprehensive list of characters that are not valid within a URL. But after thinking about it, I decided the result looked cleaner if I got rid of all punctuation, whether they could appear in a URL or not. So my code rejects characters that could legitimately be included in the URL.
public static string ConvertTextToSlug(string s)
{
StringBuilder sb = new StringBuilder();
bool wasHyphen = true;
foreach (char c in s)
{
if (char.IsLetterOrDigit(c))
{
sb.Append(char.ToLower(c));
wasHyphen = false;
}
else if (char.IsWhiteSpace(c) && !wasHyphen)
{
sb.Append('-');
wasHyphen = true;
}
}
if (wasHyphen && sb.Length > 0)
sb.Length--;
return sb.ToString();
}
Listing 1: ConvertTextToSlug() method.
Some examples I found on the web used regular expressions. My routine is simpler. It just iterates through each character in the string
, appending it to the result if it's either a letter or a character. If I encounter a space, I append a hyphen (-).
The code takes steps to prevent consecutive hyphens, keeping the result looking cleaner. It also takes steps to prevent leading and trailing hyphens.
As you can see, it's a very simple routine. But it seems to produce good results. Of course, if you decide to name your documents this way, it'll be up to you to ensure you correctly handle different titles that resolve to the same slug.