Aligning text output by the console?
Aligning text output by the console?
What I want to do, is make the text that I output via the Console.Writeline method line up perfectly regardless of length.
Example:
// Notice that no matter the length of the text on the left,
// the text on the right is always spaced at least 5 spaces.
this is output text
this is also output text
output text
my output text
Am I going to have to write my own method for this, or does .Net contain something that I can use already?
No they are created depending on input after the content is parsed.
– user1172635
Apr 24 '12 at 18:00
4 Answers
4
Something like this should work for you. Hopefully you can adapt it to your needs.
string outputs =
"this is output",
"this is also output",
"output",
"my output"
;
// order outputs in descending order by length
var orderedOutputs = outputs.OrderByDescending(s => s.Length);
// get longest output and add 5 chars
var padWidth = orderedOutputs.First().Length + 5;
foreach (string str in outputs)
// this will pad the right side of the string with whitespace when needed
string paddedString = str.PadRight(padWidth);
Console.WriteLine("01", paddedString, "text");
Nice solution. Suggestion:
var padWidth = outputs.Max (o => o.Length) + 5;– Philip Fourie
Apr 24 '12 at 18:33
var padWidth = outputs.Max (o => o.Length) + 5;
Ah, yes, that is better indeed.
– indot_brad
Apr 24 '12 at 18:37
You can also take a look at this page that explains .NET string formatting. Instead of a manual PadLeft and PadRight you can declare the padding size directly in your formatting string. Something along the lines of
PadLeft
PadRight
var offset = outputs.Max( s => s.Length );
var formatString = "0,-" + offset + " 1";
foreach( var dataPoint in /*[your collection of data points]*/ )
Console.WriteLine( formatString, /*[first value]*/, /*[second value]*/ );
Thanks this is what I needed. Also could you tell me the most efficient way to to this with hashtable keys? Currently I am using the CopyTo method on the hashtable to dump the keys into a string array and then using the code you posted above on the array. Is there anyway to do this without putting them in an array first?
– user1172635
Apr 24 '12 at 19:54
@user1172635 Unless you are targeting .NET 1.1 code (or are working with a framework that does) you should be using a
Dictionary rather than a Hashtable. If you absolutely have to use a HashTable, there's an example of how to loop over it directly on MSDN. I would also strongly recommend getting in the habit of looking stuff up on MSDN before asking questions.– R0MANARMY
Apr 24 '12 at 20:55
Dictionary
Hashtable
HashTable
Think in Linq instead!
var outputs = new List<string>()
"this is output",
"this is also output",
"output",
"my output"
;
var size = outputs.Max (str => str.Length) + 5;
Console.WriteLine (
string.Join(Environment.NewLine,
outputs.Select (str => str.PadRight( size ) + "Text" ) )
);
/*
this is output Text
this is also output Text
output Text
my output Text
*/
Why do it that way?
– user1172635
Apr 24 '12 at 20:14
Linq is a very dynamic way of visualizing and processing data. Use of the extension methods off of IEnumerable such as Max and really the power of Select to transform data into unique projections are quite powerful. I no longer program using the foreach statement because I only think in how to manipulate lists of data using the extension methods. It is quite powerful.
– ΩmegaMan
Apr 25 '12 at 13:51
Yeah, loops are for suckers who might want to have side effects in their code like output to screen or file.
– R0MANARMY
Apr 25 '12 at 17:50
Garbage in...garbage out...your mileage may vary.
– ΩmegaMan
Apr 25 '12 at 21:06
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
class Program
static void Main(string args)
string lines = File.ReadAllLines("E:\Exp2Act1.txt");
int what1 = (Console.WindowWidth / 2) - 18;
int here1 = (Console.WindowHeight / 3);
Console.SetCursorPosition(what1, here1);
Console.WriteLine(lines[0]);
int what2 = (Console.WindowWidth / 2) - 18;
int here2 = (Console.WindowHeight / 3) + 1;
Console.SetCursorPosition(what2, here2);
Console.WriteLine(lines[1]);
int what3 = (Console.WindowWidth / 2) - 18;
int here3 = (Console.WindowHeight / 3) + 2;
Console.SetCursorPosition(what3, here3);
Console.WriteLine(lines[2]);
int what4 = (Console.WindowWidth / 2) - 18;
int here4 = (Console.WindowHeight / 3) + 3;
Console.SetCursorPosition(what4, here4);
Console.WriteLine(lines[3]);
int what5 = (Console.WindowWidth / 2) - 18;
int here5 = (Console.WindowHeight / 3) + 4;
Console.SetCursorPosition(what5, here5);
Console.WriteLine(lines[4]);
int what6 = (Console.WindowWidth / 2) - 18;
int here6 = (Console.WindowHeight / 3) + 5;
Console.SetCursorPosition(what6, here6);
Console.WriteLine(lines[5]);
Console.Read();
That's my answer but I cannot make the (*) to be align what shall I do?
– Christine Joy Olmilla
Sep 3 at 6:19
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you have all of left column values ahead of time? If so you an just find the longest one and use standard string formatting.
– R0MANARMY
Apr 24 '12 at 17:59