Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Converting Text to a URL Slug

0.00/5 (No votes)
13 May 2010CPOL2 min read 14.9K  
How to convert text to a URL slug

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.

C#
/// <summary>
/// Creates a "slug" from text that can be used as part of a valid URL.
/// 
/// Invalid characters are converted to hyphens. Punctuation that is
/// perfect valid in a URL is also converted to hyphens to keep the
/// result mostly text. Steps are taken to prevent leading, trailing,
/// and consecutive hyphens.
/// </summary>
/// <param name="s">String to convert to a slug</param>
/// <returns></returns>
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;
    }
  }
  // Avoid trailing hyphens
  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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)