here is an improved version of the Slug generator from Kamran Ayub. You can found original post here : http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx
I?ve added the ability to remove accent.
public static string GenerateSlug(this string phrase)
{
string str = phrase.RemoveAccent().ToLower();
str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars
str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
str = Regex.Replace(str, @"\s", "-"); // hyphens
return str;
}
public static string RemoveAccent(this string txt)
{
byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
return System.Text.Encoding.ASCII.GetString(bytes);
}
note: I?ve chosen to implement them as extension method, feel free to modify this.
Thursday 09 April 2009 | Predicate<T>