Skip to content

Instantly share code, notes, and snippets.

@mrstebo
Created August 23, 2016 10:34
Show Gist options
  • Select an option

  • Save mrstebo/c4fc8d29026142806ce8c25e41bb4597 to your computer and use it in GitHub Desktop.

Select an option

Save mrstebo/c4fc8d29026142806ce8c25e41bb4597 to your computer and use it in GitHub Desktop.
A nice little method for Slugifying a string
using System.Text.RegularExpressions;
namespace mrstebo
{
internal static class StringExtensions
{
public static string Slugify(this string s)
{
return s
.ReplaceSpecialSymbols()
.RemoveSymbols()
.ReplaceSpacesWithDashes()
.ToLowerInvariant();
}
private static string ReplaceSpecialSymbols(this string s)
{
return s.Replace("&", "and");
}
private static string RemoveSymbols(this string s)
{
var re = new Regex(@"[^a-zA-Z0-9 -]");
return re.Replace(s, "");
}
private static string ReplaceSpacesWithDashes(this string s)
{
var re = new Regex(@"\s\s*");
return re.Replace(s, "-");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment