4

I'm displaying GUID's and Numbers in a simple text output report. How can i keep the length of each string 'fixed'.

eg. Currently, this is what is happening. (BAD).

[WaWorkerHost.exe] +-----------------------------------------------------------+
[WaWorkerHost.exe] +  RavenDb Initialization Report                            +
[WaWorkerHost.exe] +  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                            +
[WaWorkerHost.exe] +  o) Tenant Id: 50f1bf7f-7936-4aa9-aeca-e47b1d61bb85       +
[WaWorkerHost.exe] +  o) Number of Documents: 87                            +
[WaWorkerHost.exe] +  o) Number of Indexes: 5                            +
[WaWorkerHost.exe] +  o) Number of ~Stale Indexes: 0                            +
[WaWorkerHost.exe] +-----------------------------------------------------------+

and what i'm after...

[WaWorkerHost.exe] +-----------------------------------------------------------+
[WaWorkerHost.exe] +  RavenDb Initialization Report                            +
[WaWorkerHost.exe] +  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                            +
[WaWorkerHost.exe] +  o) Tenant Id: 50f1bf7f-7936-4aa9-aeca-e47b1d61bb85       +
[WaWorkerHost.exe] +  o) Number of Documents: 87                               +
[WaWorkerHost.exe] +  o) Number of Indexes: 5                                  +
[WaWorkerHost.exe] +  o) Number of ~Stale Indexes: 0                           +
[WaWorkerHost.exe] +-----------------------------------------------------------+

cheers!

(note: the guid is a fixed length, so that line has 'hardcoded' spaces.

4

3 回答 3

5

使用字符串格式:

static string BoxLine(int totalWidth, string format, params object[] args)
{
    string s = String.Format(format, args);
    return "+ " + s.PadRight(totalWidth - 4) + " +";
}

static string BoxStartEnd(int totalWidth)
{
    return "+" + new String('-',totalWidth-2) + "+";
}

像这样称呼它,String.Format但里面有宽度:

static void Main(string[] args)
{
    const int BoxWidth = 40;

    Console.WriteLine( BoxStartEnd(BoxWidth) );
    Console.WriteLine( BoxLine(BoxWidth, "Does this work: {0} {1}", 42, 64) );
    Console.WriteLine( BoxLine(BoxWidth, " -->Yep<--") );
    Console.WriteLine( BoxStartEnd(BoxWidth) );

    Console.Read();    
}

输出:

+--------------------------------------+
+ Does this work: 42 64                +
+  -->Yep<--                           +
+--------------------------------------+
于 2013-05-06T04:31:46.657 回答
3

很难给出确切的解决方案,因为您没有提供源代码上下文。考虑下一个代码作为构建报告的示例:

var header = string.Format("Tenant Id: {0,-43}+", Guid.NewGuid());

var docCount = 87;
var indexesCount = 5;
var staleIndexesCount = 0;


string secondRow = string.Format("Number of Documents: {0,-33}+", docCount);
string thirdRow = string.Format("Number of Indexes: {0,-35}+", indexesCount);
string fourthRow = string.Format("Number of of ~Stale Indexes: {0,-25}+", staleIndexesCount);

Console.WriteLine (header);
Console.WriteLine (secondRow);
Console.WriteLine (thirdRow);
Console.WriteLine (fourthRow);

将打印:

Tenant Id: 90814671-a5e6-48c6-ad4a-a816e7611c65       +
Number of Documents: 87                               +
Number of Indexes: 5                                  +
Number of of ~Stale Indexes: 0                        +
于 2013-05-06T04:34:36.823 回答
0

你必须知道你的目标宽度。

计算后,使用PadRight获取所需长度的字符串。您也可以使用string.Format填充支持,但如果您的宽度不固定,它可能会导致代码的可维护性降低。

于 2013-05-06T04:33:52.540 回答