How to format a number to a fixed row size in .NET?

I show GUID and Numbers in a simple text report. How can I keep the length of each string "fixed".

eg. This is currently what is happening. (Dietary supplement).

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

Hooray!

(note: guid is a fixed length, so the string has β€œhard-coded” spaces.

+4
source share
3 answers

With string formatting:

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

Name it the same as String.Format , but with a width there:

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

Output:

 +--------------------------------------+ + Does this work: 42 64 + + -->Yep<-- + +--------------------------------------+ 
+5
source

It is difficult to give an exact solution because you did not specify the context of the source code. Consider the following code as an example of building a report:

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

will print:

 Tenant Id: 90814671-a5e6-48c6-ad4a-a816e7611c65 + Number of Documents: 87 + Number of Indexes: 5 + Number of of ~Stale Indexes: 0 + 
+3
source

You must know the width you are striving for.

After you PadRight this out, use PadRight to get the string of the required length. You can also use support for string.Format support, but this can lead to a decrease in the number of supported codes if your width is not fixed.

0
source

Source: https://habr.com/ru/post/1479293/


All Articles