Skip to content

Instantly share code, notes, and snippets.

@andrijac
Created May 8, 2013 06:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrijac/5538576 to your computer and use it in GitHub Desktop.
Save andrijac/5538576 to your computer and use it in GitHub Desktop.
Replace unicode characters (that may pop up in text that is sometimes pasted by user in input field from weird sources) with appropriate ANSI equivalent.
public static class UnicodeHelper
{
public static readonly Dictionary<string, string> CharacterPairs = new Dictionary<string, string>()
{
{"’", "'"}, // UTF-8: E2 80 99
{"‘", "'"}, // UTF-8: E2 80 98
{"‚", ","}, // UTF-8: E2 80 9A
{"‛", "'"},
{"“", "\""}, // UTF-8: E2 80 9C
{"”", "\""}, // UTF-8: E2 80 9D
{"„", "\""}, // UTF-8: E2 80 9E
{"‟", "\""},
{"′", "'"},
{"″", "\""},
{"‴", "\""},
{"‵", "'"},
{"‶", "\""},
{"‷", "\""},
{"–", "-"}, // UTF-8: E2 80 93
{"‐", "-"},
{" ", "-"},
{"‒", "-"},
{"—", "-"}, // UTF-8: E2 80 94
{"―", "-"} // UTF-8: E2 80 95
};
public static string FixCharacters(string text)
{
StringBuilder sb = new StringBuilder(text);
foreach (KeyValuePair<string, string> kvp in CharacterPairs)
{
sb.Replace(kvp.Key, kvp.Value);
}
return sb.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment