gpt4 book ai didi

c# - 列外的格式化字符串 "spilling"

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:47 25 4
gpt4 key购买 nike

我是 C# 的新手,不知道要搜索什么。我正在尝试在另一个字符串列表右侧的“列”中打印一个字符串列表。右侧“列”中的两个字符串具有多个值,每个值都需要自己的一行。但是,当我尝试这样做时,需要自己的行的值一直在左侧结束,而不是停留在它们的“列”内。

这是我想看到的:

这是我得到的:

这是我的代码:

static void Main(string[] args)
{
string ut = "1Y, 4D, 01:23:45";
string met = "T+4D, 01:11:32";
string vesselName = "Saturn K";
string vesselModules = "Command/Service Module \nMunar Excursion Module";
string kerbals = "Valentina Kerman, Pilot; \nJebediah Kerman, Pilot; \nBill Kerman, Engineer; \nBob Kerman, Scientist";

string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" };
string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals };

for (int index = 0; index < headerNames.Length; index++)
Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]);
}

最佳答案

问题是您在一列中有多个项目。它可以通过拆分项目而不是创建新行来轻松解决。请注意,我使用的是 ; 而不是 \n

    string ut = "1Y, 4D, 01:23:45";
string met = "T+4D, 01:11:32";
string vesselName = "Saturn K";
string vesselModules = "Command/Service Module;Munar Excursion Module";
string kerbals = "Valentina Kerman, Pilot;Jebediah Kerman, Pilot;Bill Kerman, Engineer;Bob Kerman, Scientist";

string[] headerNames = { "UT:", "MET:", "Vessel:", "Modules:", "Crew:" };
string[] headerData = new string[5] { ut, met, vesselName, vesselModules, kerbals };
for (int index = 0; index < headerNames.Length; index++)
{
// Split data if there are more than one.
var items = headerData[index].Split(';');
if (items.Length > 1)
{
// We got more than one item. Render first item.
Console.WriteLine("{0,-12} {1,-46}", headerNames[index], items[0]);
for (int i = 1; i < items.Length; i ++)
{
// Render the following items.
Console.WriteLine("{0,-12} {1,-46}", string.Empty, items[i]);
}

}
else
{
// Only one item. Render it as usual.
Console.WriteLine("{0,-12} {1,-46}", headerNames[index], headerData[index]);
}
}

enter image description here

根据评论编辑:

Console.WriteLine 使用 string.Format() 模式。从控制台:

// Writes out a formatted string and a new line.  Uses the same 
// semantics as String.Format.
//
public virtual void WriteLine (String format, params Object[] arg)
{
WriteLine(String.Format(FormatProvider, format, arg));
}

这基本上是说用传递的参数替换每个 {}。您可以将 {} 视为占位符。我只是说它应该使用一个空值。

例子:

Console.WriteLine("{0} - {1} - {2}", "One", "Two", "Three");

输出:

One - Two - Three

这与问题中的示例非常相似,但问题还使用 {0,-12} 定义了应显示的字符数。我所做的是简单地传递一个空字符串,例如“”。

Console.WriteLine("{0} - {1} - {2}", "One", string.Empty, "Three");

输出:

One -  - Three

在我的代码中我只是说前 12 个字符不应该被任何东西替换,但仍然使用 12 个字符:

Console.WriteLine("{0,-12} {1,-46}", string.Empty, "Some value");

您可以在此处阅读有关 string.Format 的更多信息:

http://www.dotnetperls.com/format

关于递增,我所做的是用你的值填充一个数组。如果我检测到它不止一个值,我就知道第二行(以及以下所有行)应该有一个“空标题”。通过将 string.Empty 传递给 Console.WriteLine 它将呈现一个空标题(但仍然是一个标题)。

伪代码:

Split by ';'.
Is there more than one item? If yes, goto 1. If no, goto 2.

1. Render first line with header and first data.
1.1 Render all the other lines, but with an empty header (still taking up 12 chars).

2. Render line with header and data.

关于c# - 列外的格式化字符串 "spilling",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37065191/

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