The problem with filling the line

Using the code below, the spacer does not seem to play as it should, theoretically the text "ADD this text" should start with column 21 on both lines, but in str2 it has a few extra spaces. When checking the length of both lines, the length was 20, as expected.

        string str1 = "Test".PadRight(20);
        string str2 = "Test123".PadRight(20);

        string common = "Add this text";

        MessageBox.Show(str1.Length.ToString());
        MessageBox.Show(str2.Length.ToString());

        MessageBox.Show(str1 + common + "\n" + str2 + common);


Has anyone encountered this problem before? Is there something obvious I'm missing.

Many thanks.

0
source share
2 answers

Perhaps your MessageBox shows a variable pitch font?

Try setting the font to Courier New(in any appropriate control) and see if it helps.

+4

:

    string str1 = "Test".PadRight(20, 'W');
    string str2 = "Test123".PadRight(20, 'I');
    string common = "Add this text";
    MessageBox.Show(str1.Length.ToString());
    MessageBox.Show(str2.Length.ToString());
    MessageBox.Show(str1 + common + "\n" + str2 + common);

, , , , .

0

Source: https://habr.com/ru/post/1709476/


All Articles