在C#控制台应用程序中,如何在表中使用\ n?

时间:2013-11-07 20:28:15

标签: c# variables console

初学C#问题。

我有一个控制台应用程序,其中包含三列表和即用型变量,我正在编写输出。

表格格式,以防有用:

const string table = "{0,-25}{1,-4}{2,-15:f2}";
如果WriteLine只是引号中的纯文本,

\ n可以正常工作,例如

Console.WriteLine("--------------------------------------------------\n");

但我丢失了将表中的/ n放在表格格式为WriteLine的地方,其中最后一列包含一个变量。例如:

Console.WriteLine(table, "Total cost of paint", ":", paintTotalWithVAT);

我依旧记得另一种添加变量的方法 - 顺便说一下,{0}方法保存变量然后在括号末尾声明它似乎不能在表格格式内工作 - 但是我我非常感谢一些帮助。理想情况下,我希望在变量输出之前有一个货币符号(十进制格式),这样会影响我需要的格式

用一堆空的WriteLines来填充我的输出有点麻烦..!

2 个答案:

答案 0 :(得分:0)

您可以将其放在table字符串中:

const string table = "{0,-25}{1,-4}{2,-15:f2}\n";

或者如果由于某种原因这是不可行的,你可以将它连接到WriteLine上的字符串:

Console.WriteLine(table+"\n", "Total cost of paint", ":", paintTotalWithVAT);

答案 1 :(得分:0)

你想在最后添加一个新行吗?只是在最后连接一个新字符串

Console.WriteLine(table, "Total cost of paint", ":", paintTotalWithVAT, "\n");