C# How to replace Microsoft's Smart Quotes with straight quotation marks?

后端 未结 12 1425
花落未央
花落未央 2020-12-08 00:50

My post below asked what the curly quotation marks were and why my app wouldn\'t work with them, my question now is how can I replace them when my program comes across them,

12条回答
  •  抹茶落季
    2020-12-08 01:20

    just chiming in, I had done this with Regex replace just to handle a few at a time based on what I'm replacing them with:

            public static string ReplaceWordChars(this string text)
            {
                var s = text;
                // smart single quotes and apostrophe,  single low-9 quotation mark, single high-reversed-9 quotation mark, prime
                s = Regex.Replace(s, "[\u2018\u2019\u201A\u201B\u2032]", "'");
                // smart double quotes, double prime
                s = Regex.Replace(s, "[\u201C\u201D\u201E\u2033]", "\"");
                // ellipsis
                s = Regex.Replace(s, "\u2026", "...");
                // em dashes
                s = Regex.Replace(s, "[\u2013\u2014]", "-");
                // horizontal bar
                s = Regex.Replace(s, "\u2015", "-");
                // double low line
                s = Regex.Replace(s, "\u2017", "-");
                // circumflex
                s = Regex.Replace(s, "\u02C6", "^");
                // open angle bracket
                s = Regex.Replace(s, "\u2039", "<");
                // close angle bracket
                s = Regex.Replace(s, "\u203A", ">");
                // weird tilde and nonblocking space
                s = Regex.Replace(s, "[\u02DC\u00A0]", " ");
                // half
                s = Regex.Replace(s, "[\u00BD]", "1/2");
                // quarter
                s = Regex.Replace(s, "[\u00BC]", "1/4");
                // dot
                s = Regex.Replace(s, "[\u2022]", "*");
                // degrees 
                s = Regex.Replace(s, "[\u00B0]", " degrees");
    
                return s;
            }
    

    Also a few more replacements in there.

提交回复
热议问题