Sometimes you want to output multiple lines in a kind of table form to the console or a file. The main problem here is, that you need to know for each column the maximum number of characters any element has so that you can pad them correctly.

So take this example:

Title Name Street
Mr. Roman Sesamstreet
Mrs. Claudia Abbey Road

wouldn’t it be nicer to look like:

Title   Name       Street
Mr.     Roman      Sesamstreet
Mrs.    Claudia    Abbey Road

To make it easier, I created a small function which helps in doing so:

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

A sample on how to use it would be:

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

Leave a Reply

Your email address will not be published.

*