Created
May 8, 2013 06:11
-
-
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.
This file contains 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
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