题
使用填充下面给出的代码似乎并没有被打,因为它在理论上应当文字的“添加此文字” 应列21这两个字符串,但STR2启动有一些额外的空间。上检查两个字符串的长度的长度被证明是相同的20按预期方式。 结果
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);
结果
之前遇到这个问题有人?有什么明显的我失踪。
非常感谢。
解决方案
也许你的MessageBox是示出可变间距字体?
尝试设置的字体Courier New
(在任何相关的控制),以及看它是否会有所帮助。
其他提示
代码更改为:
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);
这样,你会看到,如果被正确填充字符正确的号码,你也可以说,如果是因为其他已经指出的字体宽度的问题。
不隶属于 StackOverflow