问题
Is There A Way To Format White-space Like This In C#?
I would like to know if you could parse string arrays into a method and get it to return you a formatted string with white-spaces, there is a specific way i need this done so here's an example:
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);
Output would be:
Name: John Jones.
Date of birth: 07/11/1989.
Age: 29 Years old.
As you see above in the output everything is lined up and with 5 spaces for padding as defined when we called the method. This is exactly what we want. Also we have a 2 dimensional array because that allows us to parse in more than 1 line at once. Here's another example, this time with more than two columns:
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);
Second Output would be:
Name: John Jones.
Date of birth: Monday, 07/11/1989.
Age: 29 Years old.
That's all i would like to know if you know of anything that can help me please let me know. Thanks for your help, you guys really made my day.
回答1:
I personally love using Linq, also this works with any number of columns and will calculate the distance needed for each column.
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))));
}
回答2:
Try something like this (I added an extra row to your data to make it not square and make debugging more obvious):
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("");
}
}
}
The result looks like:
Name: John Jones.
Date of birth: Monday, 07/11/1989.
Age: 29 Years old.
Eye Color: blue
来源:https://stackoverflow.com/questions/53401292/question-is-there-a-way-to-format-white-space-like-this-in-c