Convert a string/integer to superscript in C#

后端 未结 1 702
旧巷少年郎
旧巷少年郎 2020-12-11 15:14

Is there a built in .NET function or an easy way to convert from:

\"01234\"

to:

\"\\u2070\\u00B9\\u00B2\\u00B3\\u2074\"


        
相关标签:
1条回答
  • 2020-12-11 15:50

    EDIT: I hadn't noticed that the superscript characters weren't as simple as \u2070-\u2079. You probably want to set up a mapping between characters. If you only need digits, you could just index into a string fairly easily:

    const string SuperscriptDigits = 
        "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
    

    Then using LINQ:

    string superscript = new string(text.Select(x => SuperscriptDigits[x - '0'])
                                        .ToArray());
    

    Or without:

    char[] chars = text.ToArray();
    for (int i = 0; i < chars.Length; i++)
    {
        chars[i] = SuperscriptDigits[chars[i] - '0'];
    }
    string superscript = new string(chars);
    
    0 讨论(0)
提交回复
热议问题