C# Create a subscript in a string

后端 未结 1 852
不知归路
不知归路 2021-01-14 08:16

Currently I am trying to fix some formatting in my application. I am trying to subscript a string and append it to a normal string- the same way yo

相关标签:
1条回答
  • 2021-01-14 08:44

    First of all there are limeted number of symbols which can be used for subscription. There are these symbols:

    1 - '\u2081'
    2-  '\u2082'
    3-  '\u2083'
    ...
    9 - '\u2089'
    + - '\u208A'
    - - '\u208B'
    = - '\u208C'
    ( - '\u208D'
    ) - '\u208E'
    

    That's all. So you can't subscript the string like "SubscriptedText".

    If you want convert to subscription some digit or allowed symbol you can try the following way:

    void ShowSubText()
        {
            String inputString = "NormalText";
            var nonDigitSymbolsTable = new Dictionary<char, char>();
            nonDigitSymbolsTable.Add('+', 'A');
            nonDigitSymbolsTable.Add('-', 'B');
            nonDigitSymbolsTable.Add('=', 'C');
            nonDigitSymbolsTable.Add('(', 'D');
            nonDigitSymbolsTable.Add(')', 'E');
            StringBuilder temp = new StringBuilder();
            int checkToDigit = 0;
            foreach (char t in "1234567890+-=()".ToCharArray())
            {
                if (int.TryParse(t.ToString(), out checkToDigit))
                    temp.Append("\\u208" + t);
                else
                    temp.Append("\\u208" + nonDigitSymbolsTable[t]);
            }
    
            MessageBox.Show(inputString + GetStringFromUnicodeSymbols(temp.ToString()));
        }
        string GetStringFromUnicodeSymbols(string unicodeString)
        {
            var stringBuilder = new StringBuilder();
            foreach (Match match in Regex.Matches(unicodeString, @"\\u(?<Value>[a-zA-Z0-9]{4})"))
            {
                stringBuilder.AppendFormat(@"{0}",
                                           (Char)int.Parse(match.Groups["Value"].Value,System.Globalization.NumberStyles.HexNumber));
            }
    
            return stringBuilder.ToString();
        }
    
    0 讨论(0)
提交回复
热议问题