Aligning text output by the console?
Asked Answered
C

4

4

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?

Cabana answered 24/4, 2012 at 17:54 Comment(2)
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.Dimitri
No they are created depending on input after the content is parsed.Cabana
J
3

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
*/
Jotunheim answered 24/4, 2012 at 19:48 Comment(3)
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.Guanajuato
Yeah, loops are for suckers who might want to have side effects in their code like output to screen or file.Dimitri
Garbage in...garbage out...your mileage may vary.Guanajuato
C
2

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("{0}{1}", paddedString, "text");
}
Chatoyant answered 24/4, 2012 at 18:24 Comment(2)
Nice solution. Suggestion: var padWidth = outputs.Max (o => o.Length) + 5;Switchback
Ah, yes, that is better indeed.Chatoyant
D
2

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

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]*/ );
}
Dimitri answered 24/4, 2012 at 18:53 Comment(2)
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?Cabana
@Cabana 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.Dimitri
Y
0
    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();
    }
}

}

Yarn answered 3/9, 2018 at 6:17 Comment(1)
That's my answer but I cannot make the (*) to be align what shall I do?Yarn

© 2022 - 2024 — McMap. All rights reserved.