Display formatted text on console using padding(在控制台上使用填充显示格式化文本)
问题描述
我计划以类似于以下格式的方式为我的控制台应用程序的参数编写一份说明:
The following options are possible:
myOption: Text do describe the option, but that should be splitted
to several lines if too big. Text should automatically
align by a fixed offset.
我已经有了一种在正确位置拆分文本的方法(假设我们不关心是否在任何单词的中间拆分,只有在我们真正关心在单词边界处拆分时才会牵涉到事情)。然而,我仍然坚持对选项的解释。
这是目前为止的代码:
public void DisplayHelpEx()
{
var offset = this._options.Max(x => x.ParameterName.Length) + 6;
Console.WriteLine("The following options are possible:");
foreach (var option in this._corrections)
{
Console.Write((option.ParameterName + ": ").PadLeft(offset));
WriteOffset(offset, option.Explanation);
}
}
public void WriteOffset(int offset, string text)
{
var numChars = TOTAL_NUMBER_CHARS_PER_LINE - offset;
string line;
while ((line = new String(text.Take(numChars).ToArray())).Any())
{
var s = line.PadLeft(numChars);
Console.Write(s);
Console.WriteLine();
text= new String(text.Skip(numChars).ToArray());
}
}
我尝试了许多.PadLeft
和.PadRight
的组合,但都不能正常工作。
使用上面的方法,我得到以下输出:
The following options are possible:
myOption: Text do describe the option, but that should be splitted
to several lines if too big. Text should automatically
align by a fixed offset.
推荐答案
PadLeft采用文本并向左或向右添加一些空格,以使全文具有定义的宽度,请参见https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx。
然而,在您的例子中,您不希望整个文本具有固定的宽度(特别是如果您将来想要在单词边界处很好地拆分),而是让偏移量位于开头。那么为什么不像这样在每一行的开头添加偏移量空格呢?
private const string optionParameterName = "myOption";
private const string optionText =
"Text do describe the option, but that should be splitted to several lines if too big.Text should automatically align by a fixed offset.";
private const int TOTAL_NUMBER_CHARS_PER_LINE = 60;
public void DisplayHelpEx()
{
var offset = optionParameterName.Length + 6;
Console.WriteLine("The following options are possible:");
WriteOffset(offset, optionParameterName + ": ", optionText);
}
public void WriteOffset(int offset, string label, string text)
{
var numChars = TOTAL_NUMBER_CHARS_PER_LINE - offset;
string offsetString = new string(' ', offset);
string line;
bool firstLine = true;
while ((line = new String(text.Take(numChars).ToArray())).Any())
{
if (firstLine)
{
Console.Write(label.PadRight(offset));
}
else
{
Console.Write(offsetString);
}
firstLine = false;
Console.Write(line);
Console.WriteLine();
text = new String(text.Skip(numChars).ToArray());
}
}
// output:
// The following options are possible:
// myOption: Text do describe the option, but that should b
// e splitted to several lines if too big.Text sh
// ould automatically align by a fixed offset.
请注意,我在第一行使用了Label.PadRight(偏移量),以确保将带有标签的字符串填充到正确的长度--在这里,填充很有用,因为它允许我们使标签字符串具有与其他偏移量完全相同的宽度。
这篇关于在控制台上使用填充显示格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在控制台上使用填充显示格式化文本
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01