Created
August 23, 2016 10:34
-
-
Save mrstebo/c4fc8d29026142806ce8c25e41bb4597 to your computer and use it in GitHub Desktop.
A nice little method for Slugifying a string
This file contains hidden or 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
| 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