如何在C#中的string.Format中对齐? [英] How to do Alignment within string.Format in C#?

查看:156
本文介绍了如何在C#中的string.Format中对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有以下代码行:

 返回字符串.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}",名称,CPSA,邮政编码,等级,Score1,Score2,Score3,Score4,Score5,Score6,Score7,Score8); 

它从文本文件中提取数据,并在列表框中输出.我想证明其中一半在左边,一半在右边,所以在梦境世界中:

 返回字符串.Format("align = left({0},{1},{2},{3},{4},)align = right({5},{6},{7},{8},{9},{10},{11})",名称,CPSA,邮政编码,等级,Score1,Score2,Score3,Score4,Score5,Score6,Score7,Score8); 

我环顾四周,却不知道如何去做.谁能解释一下?

解决方案

您可以执行以下操作:

  Console.WriteLine(String.Format("{0,-10} | {1,5}","Bill",51)); 

您会得到"51"在5个字符上向右对齐.

此处有更多示例:将字符串与空格对齐./p>

有关官方参考,请参见复合格式

I have this line of code in C#:

return string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8);

It draws its data from a text file and is output in a list box. I want to justify half of it to the left and half to the right so in dream world this:

return string.Format("align=left({0}, {1}, {2}, {3}, {4},) align=right ({5}, {6}, {7}, {8}, {9}, {10}, {11})", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8);

I have looked around but have no clue how to do it. Can anyone please explain?

解决方案

You can do something like this:

Console.WriteLine(String.Format("{0,-10} | {1,5}", "Bill", 51));

You'll get "51" aligned to right on 5 characters.

More examples here: Align String with Spaces.

For official reference, see Composite Formatting

这篇关于如何在C#中的string.Format中对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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