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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过控制台对齐文本输出?相关的知识,希望对你有一定的参考价值。

我想要做的是使我通过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字符串格式的this页面。您可以直接在格式化字符串中声明填充大小,而不是手动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]*/ );
}
另一答案

想想在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
*/
另一答案
    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();
    }
}

}

以上是关于通过控制台对齐文本输出?的主要内容,如果未能解决你的问题,请参考以下文章

通过字母在画布上绘制文本,以控制单个字母的alpha,同时与中心对齐

第11集 PLC输入与输出控制

java标准化格式输出 在控制台输出类似表格类型的左对齐格式

textarea标签输出内容时不能顶格(左对齐)输出

如何在控制台的同一行上对齐我的项目?

文本对齐:通过 DomPDF (Laravel) 生成 PDF 居中,向右偏移一点