我想要做的是让我通过 Console.Writeline 方法输出的文本无论长度如何都完美地对齐。

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

我是否必须为此编写自己的方法,还是 .Net 包含我已经可以使用的东西?

最佳答案

而是在 Linq 中思考!

var outputs = new List<string>() {
                        "this is output",
                        "this is also output",
                        "output",
                        "my output"
                    };

var size = outputs.Max (str => str.Length) + 5;

Console.WriteLine (
           string.Join(Environment.NewLine,
                       outputs.Select (str => str.PadRight( size ) + "Text" ) )
                   );

/*
this is output          Text
this is also output     Text
output                  Text
my output               Text
*/

关于c# - 对齐控制台的文本输出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10303319/

10-14 10:12