2013-05-06 67 views
4

我在简单的文本输出报告中显示GUID和数字。我怎样才能保持每个字符串'固定'的长度。如何在.NET中将数字格式化为固定的字符串大小?

例如。目前,这是正在发生的事情。 (坏)。

[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] +-----------------------------------------------------------+ 

和我后我...

[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] +-----------------------------------------------------------+ 

喝彩!

(注:GUID是一个固定的长度,使线路具有“硬编码的”空间

回答

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<--       + 
+--------------------------------------+ 
+0

OMG。这是一个尴尬和尴尬的时刻。 – 2013-05-06 04:34:09

0

你要知道你的目标的宽度。

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

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      + 
+0

...并手动找出每个不同线路的对齐方式? – 2013-05-06 04:47:17

+0

@JonathonReinhart OP明确指出了硬编码宽度。我评论说我的解决方案只是作为一个例子。你的显然更通用和干净,这就是为什么我upvoted它... – 2013-05-06 04:52:10

相关问题