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

Community
  • 1
  • 1
Leon Bohmann
  • 402
  • 1
  • 4
  • 16
  • 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. – Matt Burland Aug 06 '15 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. – Matt Burland Aug 06 '15 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. – Matt Burland Aug 06 '15 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 Aug 06 '15 at 14:35
  • @MattBurland Okay thank you both. Could you give me a short inspiration on how I should start displaying richtext format on my own? I think about implementing a class which extends a richtextbox or something? – Leon Bohmann Aug 06 '15 at 14:47
  • Create a form, stick a `RichTextBox` on there and make it read-only. – Matt Burland Aug 06 '15 at 14:52
  • 1
    [This](http://stackoverflow.com/questions/2095583/set-superscript-and-subscript-in-formatted-text-in-wpf) may be helpful if you are using `WPF` – Hossein Narimani Rad Aug 06 '15 at 15:14
  • @HosseinNarimani I am using windows forms. Too bad, because the solution you provided would be perfect! – Leon Bohmann Aug 06 '15 at 15:17

1 Answers1

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
    Side note: you can use the same code for superscript by using the superscript range instead of subscript one (https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts) – Alexei Levenkov Feb 10 '20 at 19:56