Skip to content

Instantly share code, notes, and snippets.

@onebeatconsumer
Created November 1, 2011 01:07
Show Gist options
  • Save onebeatconsumer/1329568 to your computer and use it in GitHub Desktop.
Save onebeatconsumer/1329568 to your computer and use it in GitHub Desktop.
Slug Generator - C# - String Extension Method
/// <summary>
/// Generates a permalink slug for passed string
/// </summary>
/// <param name="phrase"></param>
/// <returns>clean slug string (ex. "some-cool-topic")</returns>
public static string GenerateSlug(this string phrase)
{
var s = phrase.RemoveAccent().ToLower();
s = Regex.Replace(s, @"[^a-z0-9\s-]", ""); // remove invalid characters
s = Regex.Replace(s, @"\s+", " ").Trim(); // single space
s = s.Substring(0, s.Length <= 45 ? s.Length : 45).Trim(); // cut and trim
s = Regex.Replace(s, @"\s", "-"); // insert hyphens
return s.ToLower();
}
@onebeatconsumer
Copy link
Author

simple slug generator extension method I use. pretty boilerplate, but I thought I'd throw it out there and experiment with Git/Gist a little more...

@MrMitch
Copy link

MrMitch commented Oct 19, 2013

What is this RemoveAccent method on line 8 ?

@alexjamesbrown
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment