字符串格式和打印 [英] string formatting and printing

查看:164
本文介绍了字符串格式和打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对齐值。我想知道为什么会发生这种情况:

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

打印(值);

如果我在打印之前检查了值,这是正确的。结果是:

pre $ value $ value $ b $ val值
code $

正如他们应该那样,但当我打印价值到我的打印机,然后他们得到这样的:

 价值价值
val价值

理解为什么当我打印文本时会改变字符串?



我试图使用\ t,但是我的打印机似乎不理解\ t,因为这些标签没有打印出来。
$ b btw:这只是一个测试代码,所以你可以理解我用真正的代码所遇到的问题。

解决方案

您的控制台使用固定宽度的字体,您的打印机没有(至少在默认情况下)。所以空间占用打印机的空间较少,字母会根据实际宽度占用更多或更少的空间。

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.

解决方案

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.

这篇关于字符串格式和打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆