Created
June 16, 2012 15:20
-
-
Save timiles/2941647 to your computer and use it in GitHub Desktop.
String helper: to url fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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