4

我想要做的是使我通过 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 是否包含我已经可以使用的东西?

4

4 回答 4

3

改为在 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
*/
于 2012-04-24T19:48:47.217 回答
2

像这样的东西应该适合你。希望您可以根据自己的需要进行调整。

string[] outputs = {
                        "this is output",
                        "this is also output",
                        "output",
                        "my output"
                    };

// order outputs in descending order by length
var orderedOutputs = outputs.OrderByDescending(s => s.Length);

// get longest output and add 5 chars
var padWidth = orderedOutputs.First().Length + 5;

foreach (string str in outputs)
{
    // this will pad the right side of the string with whitespace when needed
    string paddedString = str.PadRight(padWidth);
    Console.WriteLine("{0}{1}", paddedString, "text");
}
于 2012-04-24T18:24:54.893 回答
2

您还可以查看解释 .NET 字符串格式的此页面。而不是手册PadLeftPadRight您可以直接在格式化字符串中声明填充大小。类似的东西

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]*/ );
}
于 2012-04-24T18:53:00.620 回答
0
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace ConsoleApplication1
    {
     class Program
    {
    static void Main(string[] args)
    {

        string[] lines = File.ReadAllLines("E:\\Exp2Act1.txt");

        int what1 = (Console.WindowWidth / 2) - 18;
        int here1 = (Console.WindowHeight / 3);
        Console.SetCursorPosition(what1, here1);
        Console.WriteLine(lines[0]);
        int what2 = (Console.WindowWidth / 2) - 18;
        int here2 = (Console.WindowHeight / 3) + 1;
        Console.SetCursorPosition(what2, here2);
        Console.WriteLine(lines[1]);
        int what3 = (Console.WindowWidth / 2) - 18;
        int here3 = (Console.WindowHeight / 3) + 2;
        Console.SetCursorPosition(what3, here3);
        Console.WriteLine(lines[2]);
        int what4 = (Console.WindowWidth / 2) - 18;
        int here4 = (Console.WindowHeight / 3) + 3;
        Console.SetCursorPosition(what4, here4);
        Console.WriteLine(lines[3]);
        int what5 = (Console.WindowWidth / 2) - 18;
        int here5 = (Console.WindowHeight / 3) + 4;
        Console.SetCursorPosition(what5, here5);
        Console.WriteLine(lines[4]);
        int what6 = (Console.WindowWidth / 2) - 18;
        int here6 = (Console.WindowHeight / 3) + 5;
        Console.SetCursorPosition(what6, here6);
        Console.WriteLine(lines[5]);
        Console.Read();
    }
}

}

于 2018-09-03T06:17:57.993 回答