Last active
August 30, 2017 16:47
-
-
Save 5up3rman/e8c29036153cff80d39b59858d5eb764 to your computer and use it in GitHub Desktop.
Regex + ItemUtil + StringUtil
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
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