2009-04-18 105 views
0

使用填充下面给出的代码似乎没有按照原样播放,理论上文本“ADD this text”应该从第21列开始在两个字符串中,但在str2中它有一个几个额外的空间。在检查两个琴弦的长度时,结果与预期的相同。
字符串填充问题

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


任何人都遇到过这个问题吗?有什么明显的我缺少。

很多谢谢。

回答

4

也许你的MessageBox显示变间距字体?

尝试将字体设置为Courier New(在任何相关控件中),并查看它是否有帮助。

+0

同样的事情发生在TextBox,TextBlock等。 – 2009-04-18 10:37:21

0

你的代码更改为:

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

这样,你会看到,如果被正确填充字符正确的号码,你也可以说,如果它是一个字体宽度问题,因为其他已经声明。