Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created September 8, 2010 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjangda/570304 to your computer and use it in GitHub Desktop.
Save mjangda/570304 to your computer and use it in GitHub Desktop.
Sanitizes a given string into a slug-friendly form.
/// <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