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

后端 未结 12 1400
花落未央
花落未央 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:40

    it worked for me, you can try below code

    string replacedstring = ("your string with smart quotes").Replace('\u201d', '\'');
    

    Thanks!

    0 讨论(0)
  • 2020-12-08 01:42

    A more extensive listing of problematic word characters

    if (buffer.IndexOf('\u2013') > -1) buffer = buffer.Replace('\u2013', '-');
    if (buffer.IndexOf('\u2014') > -1) buffer = buffer.Replace('\u2014', '-');
    if (buffer.IndexOf('\u2015') > -1) buffer = buffer.Replace('\u2015', '-');
    if (buffer.IndexOf('\u2017') > -1) buffer = buffer.Replace('\u2017', '_');
    if (buffer.IndexOf('\u2018') > -1) buffer = buffer.Replace('\u2018', '\'');
    if (buffer.IndexOf('\u2019') > -1) buffer = buffer.Replace('\u2019', '\'');
    if (buffer.IndexOf('\u201a') > -1) buffer = buffer.Replace('\u201a', ',');
    if (buffer.IndexOf('\u201b') > -1) buffer = buffer.Replace('\u201b', '\'');
    if (buffer.IndexOf('\u201c') > -1) buffer = buffer.Replace('\u201c', '\"');
    if (buffer.IndexOf('\u201d') > -1) buffer = buffer.Replace('\u201d', '\"');
    if (buffer.IndexOf('\u201e') > -1) buffer = buffer.Replace('\u201e', '\"');
    if (buffer.IndexOf('\u2026') > -1) buffer = buffer.Replace("\u2026", "...");
    if (buffer.IndexOf('\u2032') > -1) buffer = buffer.Replace('\u2032', '\'');
    if (buffer.IndexOf('\u2033') > -1) buffer = buffer.Replace('\u2033', '\"');
    
    0 讨论(0)
  • 2020-12-08 01:42

    To extend on Nick van Esch's popular answer, here is the code with the names of the characters in the comments.

    if (buffer.IndexOf('\u2013') > -1) buffer = buffer.Replace('\u2013', '-'); // en dash
    if (buffer.IndexOf('\u2014') > -1) buffer = buffer.Replace('\u2014', '-'); // em dash
    if (buffer.IndexOf('\u2015') > -1) buffer = buffer.Replace('\u2015', '-'); // horizontal bar
    if (buffer.IndexOf('\u2017') > -1) buffer = buffer.Replace('\u2017', '_'); // double low line
    if (buffer.IndexOf('\u2018') > -1) buffer = buffer.Replace('\u2018', '\''); // left single quotation mark
    if (buffer.IndexOf('\u2019') > -1) buffer = buffer.Replace('\u2019', '\''); // right single quotation mark
    if (buffer.IndexOf('\u201a') > -1) buffer = buffer.Replace('\u201a', ','); // single low-9 quotation mark
    if (buffer.IndexOf('\u201b') > -1) buffer = buffer.Replace('\u201b', '\''); // single high-reversed-9 quotation mark
    if (buffer.IndexOf('\u201c') > -1) buffer = buffer.Replace('\u201c', '\"'); // left double quotation mark
    if (buffer.IndexOf('\u201d') > -1) buffer = buffer.Replace('\u201d', '\"'); // right double quotation mark
    if (buffer.IndexOf('\u201e') > -1) buffer = buffer.Replace('\u201e', '\"'); // double low-9 quotation mark
    if (buffer.IndexOf('\u2026') > -1) buffer = buffer.Replace("\u2026", "..."); // horizontal ellipsis
    if (buffer.IndexOf('\u2032') > -1) buffer = buffer.Replace('\u2032', '\''); // prime
    if (buffer.IndexOf('\u2033') > -1) buffer = buffer.Replace('\u2033', '\"'); // double prime
    
    0 讨论(0)
  • 2020-12-08 01:42

    According to the Character Map application that comes with Windows, the Unicode values for the curly quotes are 0x201c and 0x201d. Replace those values with the straight quote 0x0022, and you should be good to go.

    String.Replace(0x201c, '"');
    String.Replace(0x201d, '"');
    
    0 讨论(0)
  • 2020-12-08 01:42

    The VB equivalent of what @Matthew wrote:

    Public Module StringExtensions
    
        <Extension()>
        Public Function StripIncompatableQuotes(BadString As String) As String
            If Not String.IsNullOrEmpty(BadString) Then
                Return BadString.Replace(ChrW(&H2018), "'").Replace(ChrW(&H2019), "'").Replace(ChrW(&H201C), """").Replace(ChrW(&H201D), """")
            Else
                Return BadString
            End If
        End Function
    End Module
    
    0 讨论(0)
  • 2020-12-08 01:42

    I also have a program which does this, the source is in this file of CP-1252 Fixer. It additionally defines some mappings for converting characters within RTF strings whilst preserving all formatting, which may be useful to some.

    It is also a complete mapping of all "smart quote" characters to their low-ascii counterparts, entity codes and character references.

    0 讨论(0)
提交回复
热议问题