Created
September 8, 2010 15:36
-
-
Save mjangda/570304 to your computer and use it in GitHub Desktop.
Sanitizes a given string into a slug-friendly form.
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
/// <summary> | |
/// Cleans up a given string into a slug-friendly form. | |
/// Strips special characters and replaces spaces with hyphens. | |
/// | |
/// Given something like "The Quick Fox Don't Like No Guff!" return "The-Quick-Fox-Dont-Like-No-Guff" | |
/// | |
/// Borrowed from WordPress. | |
/// </summary> | |
/// <param name="str">The string to be sanitized</param> | |
/// <returns>A sanitized string</returns> | |
public static string SanitizeString(string str) | |
{ | |
str = str.Replace(' ', '-'); | |
List<string> specialChars = new List<string>(){ | |
"?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}" | |
}; | |
foreach (string ch in specialChars) | |
str = str.Replace(ch, string.Empty); | |
str = str.Trim(); | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment