问题:有没有办法在C#中格式化这样的空格?

时间:2018-11-20 20:49:25

标签: c# formatting whitespace

是否可以在C#中格式化这样的空格?

我想知道是否可以将字符串数组解析为一个方法,并使其返回带有空格的格式化字符串,所以有一种特定的方式需要我这样做,这是一个示例:

string[][] StringArray = new string[][] {
    new string[] {"Name:", "John Jones."}
    new string[] {"Date of birth:", "07/11/1989."}
    new string[] {"Age:", "29 Years old."}
};

FormatWhiteSpace(StringArray, Padding: 5);

输出为:

Name:              John Jones.
Date of birth:     07/11/1989.
Age:               29 Years old.

如您在上面的输出中所见,所有内容都已对齐,并有5个空格用于填充,这是我们在调用该方法时定义的。这正是我们想要的。我们还有一个二维数组,因为它允许我们一次解析多于1行。 这是另一个示例,这次有两个以上的列:

string[][] StringArray = new string[][] {
    new string[] {"Name:", "John", "Jones."}
    new string[] {"Date of birth:", "Monday,", "07/11/1989."}
    new string[] {"Age:", "29", "Years old."}
};

FormatWhiteSpace(StringArray, Padding: 2);

第二个输出将是:

Name:           John     Jones.
Date of birth:  Monday,  07/11/1989.
Age:            29       Years old.
  

这就是我想知道的一切,如果您知道有什么可以帮助您的   我,请让我知道。谢谢您的帮助,你们真的让我   天。

2 个答案:

答案 0 :(得分:0)

尝试这样的操作(我在您的数据中添加了额外的一行,以使其不成方形,并使调试更加明显):

public static class PaddedColumns
{
    private static string[][] _stringArray = new string[][] {
        new [] {"Name:", "John", "Jones."},
        new [] {"Date of birth:", "Monday,", "07/11/1989."},
        new [] {"Age:", "29", "Years old."},
        new [] {"Eye Color:", "blue", ""},
    };

    public static void PadIt()
    {
        OutputPadded(_stringArray);
    }

    public static void OutputPadded(string[][] strings)
    {
        var columnMaxes = new int[strings[0].Length];

        foreach (var row in strings)
        {
            for (var colNo = 0; colNo < row.Length; ++colNo)
            {
                if (row[colNo].Length > columnMaxes[colNo])
                {
                    columnMaxes[colNo] = row[colNo].Length;
                }
            }
        }

        const int extraPadding = 2;

        //got the maxes, now go through and use them to pad things
        foreach (var row in strings)
        {
            for (var colNo = 0; colNo < row.Length; ++colNo)
            {
                Console.Write(row[colNo].PadRight(columnMaxes[colNo] + extraPadding));
            }
            Console.WriteLine("");
        }

    }

}

结果如下:

Name:           John     Jones.
Date of birth:  Monday,  07/11/1989.
Age:            29       Years old.
Eye Color:      blue

答案 1 :(得分:0)

我个人很喜欢使用Linq,它也适用于任意数量的列,并将计算每列所需的距离。

void Main()
{
    string[][] StringArray = new string[][] {
    new [] {"Name:", "John", "Jones."},
    new [] {"Date of birth:", "Monday,", "07/11/1989."},
    new [] {"Age:", "29", "Years old."}};

    var lines = FormatWhiteSpace(StringArray, Padding: 2);

    foreach (var line in lines)
    {
        Console.WriteLine(line);
    }
}

private IEnumerable<string> FormatWhiteSpace(string[][] StringArray, int Padding)
{
    var maxLengthDict = StringArray
        .Select(sa => sa.Select((s, i) => new { Column = i, MaxLength = s.Length }))        
        .SelectMany(x => x)
        .GroupBy(x => x.Column)
        .Select(x => new {Column = x.Key, MaxLength = x.Max(y => y.MaxLength)})
        .ToDictionary(x => x.Column, x => x.MaxLength);

    return StringArray
        .Select(sa => string.Concat(sa.Select((s, i) => s.PadRight(maxLengthDict[i] + Padding))));
}