如何打印列表作为表中的控制台应用程序? [英] How to print List as table in console application?

查看:126
本文介绍了如何打印列表作为表中的控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的方法来打印列表作为表中的控制台应用程序和preVIEW在方便的格式是这样的:

  Pom_No ITEM_ code ordered_qty received_qty

1011 ITEM_ code1 ordered_qty1 received_qty1

1011 ITEM_ code2 ordered_qty2 received_qty2

1011 ITEM_ code3 ordered_qty3 received_qty3

1012 ITEM_ code1 ordered_qty1 received_qty1

1012 ITEM_ code2 ordered_qty2 received_qty2

1012 ITEM_ code3 ordered_qty3 received_qty3
 

解决方案

您主要手段将是

  Console.WriteLine({0,5} {1,10} {2} -10,S1,S2,S3);
 

5 10 的宽度说明。使用负值左对齐。

格式也是可能的:

  Console.WriteLine(Y = {0,12:#,## 0.00},Y);
 

或24和自定义格式宽度的日期:

 的String.Format(现在= {0,24:DD HH:MM:SS},DateTime.Now);
 


编辑,对于C#6

通过串插您现在还可以写

  Console.WriteLine(${s1,5} {s2,10} {S3,-10});
Console.WriteLine($Y = {Y,12#,## 0.00});
的String.Format($现在= {DateTime.Now,24:DD HH:MM:SS});
 

此并不限于只是的WriteLine()或的ToString()

 字符串s = $现在= {DateTime.Now,24:DD HH:MM:SS}++ $Y = {Y,12:#,# #0.00};
 

I need Method to print List as table in console application and preview in convenient format like this:

Pom_No          Item_Code          ordered_qty                      received_qty

1011            Item_Code1         ordered_qty1                    received_qty1 

1011            Item_Code2         ordered_qty2                    received_qty2

1011            Item_Code3         ordered_qty3                    received_qty3

1012            Item_Code1         ordered_qty1                    received_qty1 

1012            Item_Code2         ordered_qty2                    received_qty2

1012            Item_Code3         ordered_qty3                    received_qty3

解决方案

Your main tool would be

Console.WriteLine("{0,5} {1,10} {2,-10}", s1, s2, s3);  

The ,5 and ,10 are width specifiers. Use a negative value to left-align.

Formatting is also possible:

Console.WriteLine("y = {0,12:#,##0.00}", y);

Or a Date with a width of 24 and custom formatting:

String.Format("Now = {0,24:dd HH:mm:ss}", DateTime.Now);


Edit, for C#6

With string interpolation you can now also write

Console.WriteLine($"{s1,5} {s2,10} {s3,-10}");  
Console.WriteLine($"y = {y,12:#,##0.00}");
String.Format($"Now = {DateTime.Now,24:dd HH:mm:ss}" );

this is not restricted to just WriteLine() or ToString()

string s = $"Now = {DateTime.Now,24:dd HH:mm:ss}" + " " + $"y = {y,12:#,##0.00}" ;

这篇关于如何打印列表作为表中的控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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