Skip to content

Instantly share code, notes, and snippets.

@5up3rman
Last active August 30, 2017 16:47
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 5up3rman/e8c29036153cff80d39b59858d5eb764 to your computer and use it in GitHub Desktop.
Save 5up3rman/e8c29036153cff80d39b59858d5eb764 to your computer and use it in GitHub Desktop.
Regex + ItemUtil + StringUtil
static void Main(string[] args)
{
var wildItemName = "<strong class=\"color-me-red\">Name of a Product<sup>™</sup> Version 1!!!!!</strong>";
Console.WriteLine(ToValidItemName(wildItemName));
Console.WriteLine(ToValidItemName(wildItemName, true, true));
Console.ReadKey();
}
public static string ToValidItemName(string str, bool toLower = false, bool replaceSpacesWithDashes = false)
{
// Remove HTML Tags
str = StringUtil.RemoveTags(str);
// Get Valid Name
str = ItemUtil.ProposeValidItemName(str);
// Remove multiple spaces
var regex = new Regex(@"[' ']{2,}", RegexOptions.None);
str = regex.Replace(str, " ");
if (replaceSpacesWithDashes)
str = str.Replace(" ", "-");
return toLower ? str.ToLowerInvariant() : str;
}
// Result 1: Name of a Product Version 1
// Result 2: name-of-a-product-version-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment