How can I align text in columns using Console.WriteLine?

I have a column view, but the end of the two columns does not seem to align correctly. This is the code that I have at the moment:

Console.WriteLine("Customer name " + "sales " + "fee to be paid " + "70% value " + "30% value"); for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos = DisplayPos + 1) { seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7); thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3); Console.WriteLine(customer[DisplayPos] + " " + sales_figures[DisplayPos] + " " + fee_payable[DisplayPos] + " " + seventy_percent_value + " " + thirty_percent_value); } 

I am a newbie programmer, so I do not understand all the tips, but if you have any tips, we will be very grateful!

+58
c # alignment vertical-alignment console tabs
Dec 15 '10 at 10:41
source share
5 answers

Instead of manually aligning the text in columns with arbitrary lines of spaces, you should insert the actual tabs (escape sequence \t ) in each output line:

 Console.WriteLine("Customer name" + "\t" + "sales" + "\t" + "fee to be paid" + "\t" + "70% value" + "\t" + "30% value"); for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos++) { seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7); thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3); Console.WriteLine(customer[DisplayPos] + "\t" + sales_figures[DisplayPos] + "\t" + fee_payable + "\t\t" + seventy_percent_value + "\t\t" + thirty_percent_value); } 
+11
Dec 15 '10 at 11:00
source share
— -

try it

 Console.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}", customer[DisplayPos], sales_figures[DisplayPos], fee_payable[DisplayPos], seventy_percent_value, thirty_percent_value); 

where the first number inside braces is the index, and the second is alignment. The second character indicates whether the line should be left or right aligned. Use negative numbers to align to the left.

Or look at http://msdn.microsoft.com/en-us/library/aa331875(v=vs.71).aspx

+228
Dec 15 '10 at
source share

Just add roya to the answer. In C # 6.0, you can now use string interpolation:

 Console.WriteLine($"{customer[DisplayPos],10}" + $"{salesFigures[DisplayPos],10}" + $"{feePayable[DisplayPos],10}" + $"{seventyPercentValue,10}" + $"{thirtyPercentValue,10}"); 

Actually it can be one line without extra dollars, I just think it is a little easier to read like that.

And you can also use static imports on System.Console, allowing you to do this:

 using static System.Console; WriteLine(/* write stuff */); 
+15
Dec 07 '15 at 3:34
source share

I know a very old thread, but the proposed solution was not fully automatic when there are longer lines.

Therefore, I created a small helper method that makes it fully automatic. Just go to the list of string array, where each array represents a string and each element in the array, as well as a string element.

The method can be used as follows:

 var lines = new List<string[]>(); lines.Add(new[] { "What", "Before", "After"}); lines.Add(new[] { "Name:", name1, name2}); lines.Add(new[] { "City:", city1, city2}); lines.Add(new[] { "Zip:", zip1, zip2}); lines.Add(new[] { "Street:", street1, street2}); var output = ConsoleUtility.PadElementsInLines(lines, 3); 

The helper method is as follows:

 public static class ConsoleUtility { /// <summary> /// Converts a List of string arrays to a string where each element in each line is correctly padded. /// Make sure that each array contains the same amount of elements! /// - Example without: /// Title Name Street /// Mr. Roman Sesamstreet /// Mrs. Claudia Abbey Road /// - Example with: /// Title Name Street /// Mr. Roman Sesamstreet /// Mrs. Claudia Abbey Road /// <param name="lines">List lines, where each line is an array of elements for that line.</param> /// <param name="padding">Additional padding between each element (default = 1)</param> /// </summary> public static string PadElementsInLines(List<string[]> lines, int padding = 1) { // Calculate maximum numbers for each element accross all lines var numElements = lines[0].Length; var maxValues = new int[numElements]; for (int i = 0; i < numElements; i++) { maxValues[i] = lines.Max(x => x[i].Length) + padding; } var sb = new StringBuilder(); // Build the output bool isFirst = true; foreach (var line in lines) { if (!isFirst) { sb.AppendLine(); } isFirst = false; for (int i = 0; i < line.Length; i++) { var value = line[i]; // Append the value with padding of the maximum length of any value for this element sb.Append(value.PadRight(maxValues[i])); } } return sb.ToString(); } } 

Hope this helps someone. Source from a post on my blog: http://dev.flauschig.ch/wordpress/?p=387

+6
Dec 10 '14 at 11:13
source share

You can use tabs instead of spaces between columns and / or set the maximum size for a column in format strings ...

+2
Dec 15 '10 at 11:00
source share



All Articles