gpt4 book ai didi

c# - 非常棘手/复杂的文本格式 : How can I output a string[][] as a table?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:21:03 24 4
gpt4 key购买 nike

诺贝尔奖授予任何能帮助我弄清楚我这样做是否完全错误的人。所以我想做的是获取一组字符串数组作为表格打印出来,例如

{ {"abcdefg", "hij"}, {"klm", "nopqrstuvwxyz"} }

---------------->

 ====================
abcdefg | hij
--------------------
klm | nopqrstu
| vwxyz
====================

其中有一个常量确定表格的宽度(以字符为单位)。

所以我的代码是一个完整的怪物并且不起作用:

    public static void PrintTable ( string[][] cells )
{
// Outputs content in cells matrix like

// =========================================
// cells[0][0] | cells[0][1]
// -----------------------------------------
// cells[1][0] | cells[1][1]
// -----------------------------------------
// cells[2][0] | cells[2][1]
// .
// .
// -----------------------------------------
// cells[n-1][0] | cells[n-1][1]
// ========================================

// Each cell must be able to hold at least 1 character inside
// 1 space of padding on the left and right

int linelen = OutputFormatter.OutputMaxLength; // copy of width (in characters) of lines being formatted
// Calculate widths of columns 1 and 2 (excluding the single space of padding on the
// left and right side of each):
int w1 = Math.Max(cells.Max(c => c[0].Length), linelen - 5);
int w2 = linelen - w1 - 1;
OutputFormatter.PrintChars('='); // prints a line of equals signs for the top border of the table

// print out rows:
foreach ( string[] row in cells )
{
// Make the strings corresponding to the first and second column have equal
// length by adding whitespace to the end of the shorter one:
int m1 = row[0].Length, m2 = row[1].Length;
String padding = new String(' ', Math.Abs(m1 - m2));
if ( m1 > m2 ) row[1] += padding;
else row[0] += padding;

// Print out content of row
for ( int i = 0, j = 0, n = row[0].Length; i < n && j < n; i += w1, j += w2 )
{
Console.WriteLine(" {0} | {1} ",
row[0].Substring(i,w1),
row[1].Substring(j,w2));
}
OutputFormatter.PrintChars('-'); // prints a line of dashes to separate rows
}

OutputFormatter.PrintChars('='); // prints a line of equals signs to form bottom border of table
}

有人对此有单行解决方案吗? ;)

我已经完成了调试,但在点击

后抛出异常
                Console.WriteLine(" {0} | {1} ",
row[0].Substring(i,w1),
row[1].Substring(j,w2));

当我用输入字符串测试时

{
new string[] {"CompareLastTwo","Shows difference between friends lists of last two Facebook data files in repository"},
new string[] {"AddFriendList <DataFolderPath>", "blah blah blah"}
};

所有打印出来的是

enter image description here

有人可以帮我找出我在这里犯的逻辑错误吗?有没有一种方法可以更好地利用 .NET 库来使这个优雅、紧凑、高效、智能和可读?

最佳答案

首先,最好将格式化逻辑保留在 OutputFormatter 类中,而不是仅使用其中的常量值。

以下代码应该适合您。

public class OutputFormatter
{
public const int OutputMaxLength = 16;

public static string[] Format(string output)
{
int offset = 0;
List<string> outputParsed = new List<string>();
while (offset < output.Length)
{
outputParsed.Add(output.Substring(offset, Math.Min(OutputMaxLength, output.Length - offset)));
offset += OutputMaxLength;
}
return outputParsed.ToArray();
}
}

private static string[][] strings = {
new string[] {"CompareLastTwo","Shows difference between friends lists of last two Facebook data files in repository"},
new string[] {"AddFriendList <DataFolderPath>", "blah blah blah"}
};

public static void Main(string[] args)
{
foreach (string[] pair in strings)
{
string[] value0 = OutputFormatter.Format(pair[0]);
string[] value1 = OutputFormatter.Format(pair[1]);

int maxRows = Math.Max(value0.Length, value1.Length);
string template = "{0," + OutputFormatter.OutputMaxLength + "} | {1," + OutputFormatter.OutputMaxLength + "}";
for (int row = 0; row < maxRows; row++)
{
Console.Write
(
template,
value0.Length > row ? value0[row] : null,
value1.Length > row ? value1[row] : null
);
Console.WriteLine();
}
Console.WriteLine(new string('-', OutputFormatter.OutputMaxLength * 2));
}
}

关于c# - 非常棘手/复杂的文本格式 : How can I output a string[][] as a table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com