对齐文本输出的控制台? [英] Aligning text output by the console?
本文介绍了对齐文本输出的控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的,就是让文本通过Console.WriteLine命令方法排队I输出完美的,无论长短。
What I want to do, is make the text that I output via the Console.Writeline method line up perfectly regardless of length.
Example:
// Notice that no matter the length of the text on the left,
// the text on the right is always spaced at least 5 spaces.
this is output text
this is also output text
output text
my output text
难道我将不得不写我自己的方法,这一点,还是净包含的东西,我可以用了吗?
Am I going to have to write my own method for this, or does .Net contain something that I can use already?
推荐答案
您也可以看看这页面,介绍.NET字符串格式化。取而代之的是手动的 PadLeft
和 PadRight
您可以直接在格式化字符串声明填充的大小。沿
You can also take a look at this page that explains .NET string formatting. Instead of a manual PadLeft
and PadRight
you can declare the padding size directly in your formatting string. Something along the lines of
var offset = outputs.Max( s => s.Length );
var formatString = "{0,-" + offset + "} {1}";
foreach( var dataPoint in /*[your collection of data points]*/ )
{
Console.WriteLine( formatString, /*[first value]*/, /*[second value]*/ );
}
这篇关于对齐文本输出的控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文