ToUrlString

 public static string ToUrlString(this string str) {
         if (String.IsNullOrEmpty(str)) return "";
         // Unicode Character Handling: http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx
         string stFormD = str.Trim().ToLowerInvariant().Normalize(NormalizationForm.FormD);
         var sb = new StringBuilder();
         foreach (char t in
            from t in stFormD
            let uc = CharUnicodeInfo.GetUnicodeCategory(t)
            where uc != UnicodeCategory.NonSpacingMark
            select t) {
            sb.Append(t);
         }
         return Regex.Replace(sb.ToString().Normalize(NormalizationForm.FormC), "[\\W\\s]{1,}", "-").Trim('-');
      }
Example:
MyWebItem.Name.ToUrlString()

Description

takes a string, replacing special characters and spaces with - (one dash per one or many contiguous special charachters or spaces). makes lower-case and trims. good for seo.

Details

  • Author: esp
  • Submitted on: 4/17/2011 8:45:00 AM
  • Language: C#
  • Type: System.String
  • Views: 1994

Double click on the code to select all.

 

;