简体   繁体   English

c# 如何将二维数组列与字符串对齐

[英]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.我设法通过使用\t来对齐它们,但我认为可能还有其他方法。 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.我会从您的数组元素中删除\t并只保留名称。 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.如果您希望每列都以相同的 position 开头,则需要计算每列中每个字符串的长度并找到最长的字符串。 After that append ie spaces to each string so that each one in a row has the same length.之后 append 即每个字符串的空格,以便一行中的每个字符串具有相同的长度。 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:这是一个通用解决方案,它使用 LINQ 来构建一个包含每列最大宽度的字典:

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: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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