Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Created August 30, 2016 13:58
Show Gist options
  • Save shammelburg/fc4e3110c25ab11c5e4de99a62808261 to your computer and use it in GitHub Desktop.
Save shammelburg/fc4e3110c25ab11c5e4de99a62808261 to your computer and use it in GitHub Desktop.
String Helper for Url/Descriptions
public class StringHelper
{
public struct transform
{
public string UrlChar;
public string DescChar;
}
public enum transformDirection
{
UrlToDescription,
DescriptionToUrl
}
// Tranform invalid URL characters to Descriptive text Or vice versa
public static string TransformUrlDescriptiveCharacters(string inputValue, transformDirection direction)
{
string returnValue = inputValue;
List<transform> characterTransforms = new List<transform>()
{
new transform {UrlChar = "_", DescChar = "&" },
new transform {UrlChar = "half", DescChar = "1/2" },
new transform {UrlChar = "quarter", DescChar = "1/4" },
new transform {UrlChar = "three eighths", DescChar = "3/8" },
new transform {UrlChar = " inch", DescChar = "\"" }
};
foreach (var item in characterTransforms)
{
if (direction == transformDirection.UrlToDescription)
{
if (inputValue.Contains(item.UrlChar)) { inputValue = inputValue.Replace(item.UrlChar, item.DescChar); }
}
if (direction == transformDirection.DescriptionToUrl)
{
if (inputValue.Contains(item.DescChar)) { inputValue = inputValue.Replace(item.DescChar, item.UrlChar); }
}
}
return inputValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment