Skip to content

Instantly share code, notes, and snippets.

@timiles
Created June 16, 2012 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timiles/2941647 to your computer and use it in GitHub Desktop.
Save timiles/2941647 to your computer and use it in GitHub Desktop.
String helper: to url fragment
// credit: http://www.extensionmethod.net/Details.aspx?ID=453
public static string ToUrlFragment(this string s)
{
if (String.IsNullOrEmpty(s)) return "";
// Unicode Character Handling: http://blogs.msdn.com/b/michkap/archive/2007/05/14/2629747.aspx
string stFormD = s.Trim().ToLowerInvariant().Normalize(NormalizationForm.FormD);
var sb = new StringBuilder();
foreach (var 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('-');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment