简体   繁体   中英

c# how to align 2 dimensional array columns with string

My question is how can I align columns that the word's gonna be one beneath another. I've done some simple translator and I want them to be aligned. I managed to align them by using \t but I think there might be some other way to this. This is my Code:

string[,] slownik = new string[10, 3];

slownik[0, 0] = "Klucz";
slownik[1, 0] = "Biurko";
slownik[2, 0] = "Drzewo";
slownik[3, 0] = "Liść";
slownik[4, 0] = "Łóżko";
slownik[5, 0] = "Ładowarka";
slownik[6, 0] = "Plecak";
slownik[7, 0] = "Głośnik";
slownik[8, 0] = "Szkoła";
slownik[9, 0] = "Zadanie Domowe";
slownik[0, 1] = "\t\t Schlüssel";
slownik[1, 1] = "\t Schreibtisch";
slownik[2, 1] = "\t Baum";
slownik[3, 1] = "\t\t Wedel";
slownik[4, 1] = "\t\t Bett";
slownik[5, 1] = "\t Ladegerät";
slownik[6, 1] = "\t Rucksack";
slownik[7, 1] = "\t Lautsprecher";
slownik[8, 1] = "\t Schule";
slownik[9, 1] = "Hausaufgabe";
slownik[0, 2] = "\t Key";
slownik[1, 2] = "\t Desk";
slownik[2, 2] = "\t\t Tree";
slownik[3, 2] = "\t Leaf";
slownik[4, 2] = "\t\t Bed";
slownik[5, 2] = "\t Charger";
slownik[6, 2] = "\t Backpack";
slownik[7, 2] = "\t Speaker";
slownik[8, 2] = "\t School";
slownik[9, 2] = "\t Homework";

for (int i=0; i<10; i++) { //Console.Write(" ");
                Console.WriteLine(slownik[i, 0] + " \t " + slownik[i, 1] + " \t " + slownik[i, 2]);
            }

I would remove the \t from your array elements and just have the names. Then you can either calculate a padding value based on the longest string in each column, or just hard code a padding value to space the columns apart.

Something like:

using System;

public class Program
{
    public static void Main()
    {
        string[, ] slownik = new string[10, 3];
        slownik[0, 0] = "Klucz";
        slownik[1, 0] = "Biurko";
        slownik[2, 0] = "Drzewo";
        slownik[3, 0] = "Liść";
        slownik[4, 0] = "Łóżko";
        slownik[5, 0] = "Ładowarka";
        slownik[6, 0] = "Plecak";
        slownik[7, 0] = "Głośnik";
        slownik[8, 0] = "Szkoła";
        slownik[9, 0] = "Zadanie Domowe";
        slownik[0, 1] = "Schlüssel";
        slownik[1, 1] = "Schreibtisch";
        slownik[2, 1] = "Baum";
        slownik[3, 1] = "Wedel";
        slownik[4, 1] = "Bett";
        slownik[5, 1] = "Ladegerät";
        slownik[6, 1] = "Rucksack";
        slownik[7, 1] = "Lautsprecher";
        slownik[8, 1] = "Schule";
        slownik[9, 1] = "Hausaufgabe";
        slownik[0, 2] = "Key";
        slownik[1, 2] = "Desk";
        slownik[2, 2] = "Tree";
        slownik[3, 2] = "Leaf";
        slownik[4, 2] = "Bed";
        slownik[5, 2] = "Charger";
        slownik[6, 2] = "Backpack";
        slownik[7, 2] = "Speaker";
        slownik[8, 2] = "School";
        slownik[9, 2] = "Homework";


        for (int i = 0; i <= slownik.GetUpperBound(0); i++)
        { 
            Console.WriteLine(slownik[i, 0].ToString().PadRight(25) + slownik[i, 1].ToString().PadRight(25) + slownik[i, 2]);
        }
    }
}

RESULT

Klucz                    Schlüssel                Key
Biurko                   Schreibtisch             Desk
Drzewo                   Baum                     Tree
Liść                     Wedel                    Leaf
Łóżko                    Bett                     Bed
Ładowarka                Ladegerät                Charger
Plecak                   Rucksack                 Backpack
Głośnik                  Lautsprecher             Speaker
Szkoła                   Schule                   School
Zadanie Domowe           Hausaufgabe              Homework

FIDDLE DEMO

If you want to calculate a padding out, you could try something like

// Column 1 padding
int colPad1 = 0;
for (int i = 0; i <= slownik.GetUpperBound(0); i++) 
{
    if (slownik[i, 0].ToString().Length > colPad1)
    {
        colPad1 = slownik[i, 0].ToString().Length;
    }
}

// Column 2 padding
int colPad2 = 0; 
for (int i = 0; i <= slownik.GetUpperBound(0); i++) 
{
    if (slownik[i, 1].ToString().Length > colPad2)
    {
        colPad2 = slownik[i, 1].ToString().Length;
    }
}

Then use those padded values like:

for (int i = 0; i <= slownik.GetUpperBound(0); i++)
{ 
    Console.WriteLine(slownik[i, 0].ToString().PadRight(colPad1+5) + slownik[i, 1].ToString().PadRight(colPad2+5) + slownik[i, 2]);
}

If you want each column to start at the same position you will need to calculate the length of each string in each column and find the longest string. After that append ie spaces to each string so that each one in a row has the same length. Then you can start the following column based on the max length of the previous column so you would have something like this:

var max0 = GetMaxLengthOfColumnInSlownik(0, slownik);
var max1 = GetMaxLengthOfColumnInSlownik(1, slownik);
var max2 = GetMaxLengthOfColumnInSlownik(2, slownik);

for (int i=0; i<10; i++)
{ 
    var column1String = slownik[i, 0].Length< max0? AppendSpaces(slownik[i, 0], max0); 
    var column2String = slownik[i, 1].Length< max1? AppendSpaces(slownik[i, 1], max1); 
    var column2String = slownik[i, 2].Length< max2? AppendSpaces(slownik[i, 2], max1); 

    Console.WriteLine(column1String  + "  \t " + column2String  + "  \t  " + column3String);
}

Here's a generic solution that uses LINQ to build a dictionary holding the maximum width of each column:

class Program
{

    static void Main(string[] args)
    {
        string[,] slownik = new string[10, 3];

        slownik[0, 0] = "Klucz";
        slownik[1, 0] = "Biurko";
        slownik[2, 0] = "Drzewo";
        slownik[3, 0] = "Liść";
        slownik[4, 0] = "Łóżko";
        slownik[5, 0] = "Ładowarka";
        slownik[6, 0] = "Plecak";
        slownik[7, 0] = "Głośnik";
        slownik[8, 0] = "Szkoła";
        slownik[9, 0] = "Zadanie Domowe";
        slownik[0, 1] = "Schlüssel";
        slownik[1, 1] = "Schreibtisch";
        slownik[2, 1] = "Baum";
        slownik[3, 1] = "Wedel";
        slownik[4, 1] = "Bett";
        slownik[5, 1] = "Ladegerät";
        slownik[6, 1] = "Rucksack";
        slownik[7, 1] = "Lautsprecher";
        slownik[8, 1] = "Schule";
        slownik[9, 1] = "Hausaufgabe";
        slownik[0, 2] = "Key";
        slownik[1, 2] = "Desk";
        slownik[2, 2] = "Tree";
        slownik[3, 2] = "Leaf";
        slownik[4, 2] = "Bed";
        slownik[5, 2] = "Charger";
        slownik[6, 2] = "Backpack";
        slownik[7, 2] = "Speaker";
        slownik[8, 2] = "School";
        slownik[9, 2] = "Homework";

        OutputTranslations(slownik);

        Console.Write("Press Enter to quit");
        Console.ReadLine();
    }

    public static void OutputTranslations(String[,] translations)
    {
        // generate a dictionary that has the max string length for each column
        Dictionary<int, int> columnWidths = Enumerable.Range(0, translations.GetLength(1)).ToDictionary(
            col => col, // key
            col => (Enumerable.Range(0, translations.GetLength(0)) // value
                .Select(row => translations[row, col].Length)
                .ToArray()).Max()
        );         

        // output each column with padded spacing and a gutter inbetween
        int gutterSpacing = 5;
        for (int row = 0; row <= translations.GetUpperBound(0); row++)
        {
            for (int column = 0; column <= translations.GetUpperBound(1); column++)
            {
                Console.Write(translations[row, column].PadRight(columnWidths[column]) + " ".PadRight(gutterSpacing));
            }
            Console.WriteLine();
        }
    }

}

Output:

Klucz              Schlüssel        Key
Biurko             Schreibtisch     Desk
Drzewo             Baum             Tree
Lisc               Wedel            Leaf
Lózko              Bett             Bed
Ladowarka          Ladegerät        Charger
Plecak             Rucksack         Backpack
Glosnik            Lautsprecher     Speaker
Szkola             Schule           School
Zadanie Domowe     Hausaufgabe      Homework
Press Enter to quit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM