0

Using the code given below the padding doesn't seem to be playing as it should, in theory the text "ADD this text" should start from column 21 in both the strings but in str2 it has a few extra spaces. On checking the length of both the strings the length turned out to be the same 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);


Anybody encountered this problem before? Is there something obvious I am missing.

Many Thanks.

2 Answers 2

4

Maybe your MessageBox is showing variable-pitch font?

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

Sign up to request clarification or add additional context in comments.

2 Comments

The same thing happens in a TextBox, TextBlock etc.
Use fixed-width font (monospaced) in the TextBox, TextBlock, ... etc
0

Change your code to:

    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);

That way you will see if the right number of characters are being padded correctly and you will also be able to tell if it is a font width issue as other have stated.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.