C# 通过控制台对齐文本输出?

C# 通过控制台对齐文本输出?,c#,.net,text,alignment,console.writeline,C#,.net,Text,Alignment,Console.writeline,我想做的是,让我通过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 o

我想做的是,让我通过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是否包含我已经可以使用的内容?

类似的内容应该适合您。希望你能根据自己的需要进行调整

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");
}

您还可以查看解释.NET字符串格式的页面。您可以直接在格式化字符串中声明填充大小,而不是手动的
PadLeft
PadRight
。类似于

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]*/ );
}

用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
*/
var输出=新列表(){
“这是输出”,
“这也是输出”,
“输出”,
“我的输出”
};
var size=outputs.Max(str=>str.Length)+5;
Console.WriteLine(
juin(Environment.NewLine,
输出.Select(str=>str.PadRight(大小)+“文本”))
);
/*
这是输出文本
这也是输出文本
输出文本
我的输出文本
*/

}

您是否提前获得了所有左列值?如果是这样的话,你只需要找到最长的一个,并使用标准的字符串格式。不,它们是根据内容解析后的输入创建的。很好的解决方案。建议:
var padWidth=outputs.Max(o=>o.Length)+5啊,是的,确实更好。谢谢。这是我需要的。你能告诉我用哈希表键实现这一点的最有效方法吗?目前,我正在使用哈希表上的CopyTo方法将键转储到字符串数组中,然后使用上面在数组中发布的代码。在不首先将它们放入数组的情况下,是否仍可以执行此操作?@user1172635除非您针对的是.NET 1.1代码(或使用的是这样的框架),否则您应该使用而不是。如果您必须使用一个
哈希表
,这里有一个如何直接在其上循环的示例。我还强烈建议大家养成在提问之前先在MSDN上查找资料的习惯。Linq是一种非常动态的可视化和处理数据的方法。使用IEnumerable之外的扩展方法(如Max)和Select将数据转换为唯一投影的功能非常强大。我不再使用foreach语句编程,因为我只考虑如何使用扩展方法操作数据列表。它非常强大。是的,循环是为那些可能希望在代码中产生副作用(如输出到屏幕或文件)的吸盘设计的。垃圾输入…垃圾输出…你的里程可能会有所不同。这是我的答案,但我无法使(*)对齐我该怎么办?
    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();
    }
}