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

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

    Using Nick and Barbara's answers, here is example code with performance stats for 1,000,000 loops on my machine:

    input = "shmB6BhLe0gdGU8OxYykZ21vuxLjBo5I1ZTJjxWfyRTTlqQlgz0yUtPu8iNCCcsx78EPsObiPkCpRT8nqRtvM3Bku1f9nStmigaw";
    input.Replace('\u2013', '-'); // en dash
    input.Replace('\u2014', '-'); // em dash
    input.Replace('\u2015', '-'); // horizontal bar
    input.Replace('\u2017', '_'); // double low line
    input.Replace('\u2018', '\''); // left single quotation mark
    input.Replace('\u2019', '\''); // right single quotation mark
    input.Replace('\u201a', ','); // single low-9 quotation mark
    input.Replace('\u201b', '\''); // single high-reversed-9 quotation mark
    input.Replace('\u201c', '\"'); // left double quotation mark
    input.Replace('\u201d', '\"'); // right double quotation mark
    input.Replace('\u201e', '\"'); // double low-9 quotation mark
    input.Replace("\u2026", "..."); // horizontal ellipsis
    input.Replace('\u2032', '\''); // prime
    input.Replace('\u2033', '\"'); // double prime
    

    Time: 958.1011 milliseconds

    input = "shmB6BhLe0gdGU8OxYykZ21vuxLjBo5I1ZTJjxWfyRTTlqQlgz0yUtPu8iNCCcsx78EPsObiPkCpRT8nqRtvM3Bku1f9nStmigaw";
    var inputArray = input.ToCharArray();
    for (int i = 0; i < inputArray.Length; i++)
    {
        switch (inputArray[i])
        {
            case '\u2013':
                inputArray[i] = '-';
                break;
            // en dash
            case '\u2014':
                inputArray[i] = '-';
                break;
            // em dash
            case '\u2015':
                inputArray[i] = '-';
                break;
            // horizontal bar
            case '\u2017':
                inputArray[i] = '_';
                break;
            // double low line
            case '\u2018':
                inputArray[i] = '\'';
                break;
            // left single quotation mark
            case '\u2019':
                inputArray[i] = '\'';
                break;
            // right single quotation mark
            case '\u201a':
                inputArray[i] = ',';
                break;
            // single low-9 quotation mark
            case '\u201b':
                inputArray[i] = '\'';
                break;
            // single high-reversed-9 quotation mark
            case '\u201c':
                inputArray[i] = '\"';
                break;
            // left double quotation mark
            case '\u201d':
                inputArray[i] = '\"';
                break;
            // right double quotation mark
            case '\u201e':
                inputArray[i] = '\"';
                break;
            // double low-9 quotation mark
            case '\u2026':
                inputArray[i] = '.';
                break;
            // horizontal ellipsis
            case '\u2032':
                inputArray[i] = '\'';
                break;
            // prime
            case '\u2033':
                inputArray[i] = '\"';
                break;
            // double prime
        }
    }
    input = new string(inputArray);
    

    Time: 362.0858 milliseconds

提交回复
热议问题