C# How to replace Microsoft's Smart Quotes wit

2019-01-16 18: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, how can I do this in C#? Are they special characters?

curly-quotation-marks-vs-square-quotation-marks-what-gives

Thanks

11条回答
萌系小妹纸
2楼-- · 2019-01-16 19:32

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
查看更多
倾城 Initia
3楼-- · 2019-01-16 19:36

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.

查看更多
该账号已被封号
4楼-- · 2019-01-16 19:36

Try this for smart single quotes if the above don't work:

string.Replace("\342\200\230", "'")
string.Replace("\342\200\231", "'")

Try this as well for smart double quotes:

string.Replace("\342\200\234", '"')
string.Replace("\342\200\235", '"')
查看更多
Bombasti
5楼-- · 2019-01-16 19:36

it worked for me, you can try below code

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

Thanks!

查看更多
淡お忘
6楼-- · 2019-01-16 19:41

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, '"');
查看更多
登录 后发表回答