代码之家  ›  专栏  ›  技术社区  ›  Anand Shah

字符串填充问题

  •  0
  • Anand Shah  · 技术社区  · 17 年前

    使用下面给出的代码,从理论上讲,填充似乎并没有发挥应有的作用 “添加此文本” 这两个字符串都应该从第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);
    


    以前有人遇到过这个问题吗?有什么明显的东西我错过了。

    2 回复  |  直到 17 年前
        1
  •  4
  •   gimel    17 年前

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

        2
  •  0
  •   Robin Day    17 年前

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