string formatting and printing

五迷三道 提交于 2019-12-11 05:04:36

问题


I am trying to align values. I wonder why this happen :

        string value = "";

        value += string.Format("{0,-10}", "value");
        value += string.Format("{0,5}", "value");

        value += Environment.NewLine;

        value += string.Format("{0,-8}", "val");
        value += string.Format("{0,7}", "value");

        Print(value);

If i check value before i "Print" it is correct. The result is:

value     value
val       value

As they should be, but when i print "value" to my printer then they get like this :

   value     value
   val     value

I really cant understand why it changes the string when i print the text?

I have tried to use "\t" but my printer dont seem to understand "\t" because the tabs isnt printed out.

Btw: this is just a test code so you could understand the problem that i am having with the real code.


回答1:


your console uses fixed width fonts where your printer does not (at least by default). So spaces take up less space on your printer and your letters take up more or less space based on their actual width.




回答2:


This could be caused by a font that uses different character widths. In non-fixed-width fonts, spaces are often narrower than letters and numbers, so it might seem that spaces are missing. Consider using Lucida Console or another fixed-width font.



来源:https://stackoverflow.com/questions/6454629/string-formatting-and-printing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!