4

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 you can do it in MS word. I already tried this (as supposed here and here):

string temp = "NormalText";
foreach( char t in "SubscriptedText".ToCharArray())
    temp += "\x208" + t;

MessageBox.Show(temp);

Output: NormalTextȈSȈuȈbȈsȈcȈrȈiȈpȈtȈeȈdȈTȈeȈxȈt

But, as I noted afterwards it is the font who has to support the unicode definitions. And on the internet there doesn't seem to be a font who supports all letters in supscripted format.

So, is there a way to format my text in order to subscript the second half of it? Maybe a simple function I am missing? Or is this just not possible and I have to align my subscripted text on my own?

EDIT Also tried this:

string temp = "NormalText";
foreach( char t in "SubscriptedText".ToCharArray())
    temp += "\x208" + (int)t;

MessageBox.Show(temp);

But (of course) this didn't work out at all. I've got my output looking like this:

NormalTextȈ84Ȉ105Ȉ101Ȉ102Ȉ101Ȉ114Ȉ84Ȉ101Ȉ120Ȉ11

8
  • A string in C# doesn't have subscript or superscript. That's an issue for whatever is displaying the string (and a MessageBox isn't going to do it). There are a limited subset of superscript and subscript characters in a unicode block but there isn't a general way to make a block of arbitrary characters in a string super or sub script. Commented Aug 6, 2015 at 14:25
  • If you want a MessageBox that can handle sub and super script, then you will need to create your own that can, for example, display rich text or HTML or some format of your own. Commented Aug 6, 2015 at 14:27
  • 1
    BTW, what you are doing in your code is adding the Cyrillic capital yi to your string. You are misinterpreting what the Wikipedia page is telling you. For example, \x2081 would give you a subscript 1, but it's not telling you that adding \x208 before a character makes the next character subscript. Commented Aug 6, 2015 at 14:32
  • Looks like you have Unicode characters (16 bit wide). You need to convert to string with encoding. byte[] input; string output = Encoding.Unicode.GetString(input); You can not fix the issue after the data was converted to string without using encoding. Unicode data is a mixture of one and two byte characters. A string/char is two bytes with a private property indicating if each character is one or two bytes. Once data is converted to a string improperly it is impossible in string format to fix the alignment of the one and two characters data.
    – jdweng
    Commented Aug 6, 2015 at 14:35
  • 1
    This may be helpful if you are using WPF Commented Aug 6, 2015 at 15:14

1 Answer 1

3

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();
    }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.