{"id":387,"date":"2014-12-04T15:02:52","date_gmt":"2014-12-04T14:02:52","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=387"},"modified":"2014-12-04T15:17:57","modified_gmt":"2014-12-04T14:17:57","slug":"padded-output-for-console-or-file","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=387","title":{"rendered":"Padded output of multiple lines for console or file"},"content":{"rendered":"<p>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.<\/p>\n<p>So take this example:<\/p>\n<p><code>Title Name Street<\/code><br \/>\n<code>Mr. Roman Sesamstreet<\/code><br \/>\n<code>Mrs. Claudia Abbey Road<\/code><\/p>\n<p>wouldn&#8217;t it be nicer to look like:<\/p>\n<p><code>Title \u00a0 Name \u00a0 \u00a0 \u00a0 Street<\/code><br \/>\n<code>Mr. \u00a0 \u00a0 Roman \u00a0 \u00a0 \u00a0Sesamstreet<\/code><br \/>\n<code>Mrs. \u00a0 \u00a0Claudia \u00a0 \u00a0Abbey Road<\/code><\/p>\n<p>To make it easier, I created a small function which helps in doing so:<\/p>\n<pre class=\"lang:c# decode:true \">public static class ConsoleUtility\r\n{\r\n\t\/\/\/ &lt;summary&gt;\r\n\t\/\/\/ Converts a List of string arrays to a string where each element in each line is correctly padded.\r\n\t\/\/\/ Make sure that each array contains the same amount of elements!\r\n\t\/\/\/ - Example without:\r\n\t\/\/\/ Title Name Street\r\n\t\/\/\/ Mr. Roman Sesamstreet\r\n\t\/\/\/ Mrs. Claudia Abbey Road\r\n\t\/\/\/ - Example with:\r\n\t\/\/\/ Title   Name      Street\r\n\t\/\/\/ Mr.     Roman     Sesamstreet\r\n\t\/\/\/ Mrs.    Claudia   Abbey Road\r\n\t\/\/\/ &lt;param name=\"lines\"&gt;List lines, where each line is an array of elements for that line.&lt;\/param&gt;\r\n\t\/\/\/ &lt;param name=\"padding\"&gt;Additional padding between each element (default = 1)&lt;\/param&gt;\r\n\t\/\/\/ &lt;\/summary&gt;\r\n\tpublic static string PadElementsInLines(List&lt;string[]&gt; lines, int padding = 1)\r\n\t{\r\n\t\t\/\/ Calculate maximum numbers for each element accross all lines\r\n\t\tvar numElements = lines[0].Length;\r\n\t\tvar maxValues = new int[numElements];\r\n\t\tfor (int i = 0; i &lt; numElements; i++)\r\n\t\t{\r\n\t\t\tmaxValues[i] = lines.Max(x =&gt; x[i].Length) + padding;\r\n\t\t}\r\n\r\n\t\tvar sb = new StringBuilder();\r\n\t\t\/\/ Build the output\r\n\t\tbool isFirst = true;\r\n\t\tforeach (var line in lines)\r\n\t\t{\r\n\t\t\tif (!isFirst)\r\n\t\t\t{\r\n\t\t\t\tsb.AppendLine();\r\n\t\t\t}\r\n\t\t\tisFirst = false;\r\n\r\n\t\t\tfor (int i = 0; i &lt; line.Length; i++)\r\n\t\t\t{\r\n\t\t\t\tvar value = line[i];\r\n\t\t\t\t\/\/ Append the value with padding of the maximum length of any value for this element\r\n\t\t\t\tsb.Append(value.PadRight(maxValues[i]));\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn sb.ToString();\r\n\t}\r\n}<\/pre>\n<p>A sample on how to use it would be:<\/p>\n<pre class=\"lang:c# decode:true\">var lines = new List&lt;string[]&gt;();\r\nlines.Add(new[] { \"What\", \"Before\", \"After\"});\r\nlines.Add(new[] { \"Name:\", name1, name2});\r\nlines.Add(new[] { \"City:\", city1, city2});\r\nlines.Add(new[] { \"Zip:\", zip1, zip2});\r\nlines.Add(new[] { \"Street:\", street1, street2});\r\nvar output = ConsoleUtility.PadElementsInLines(lines, 3);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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. &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-387","6":"format-standard","7":"category-csharp","8":"category-programming"},"acf":[],"views":7573,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/387"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}