1
你好我正在嘗試使我的控制檯應用程序分配更美觀愉快,通過這樣做我決定實現String.Format到我的循環。我們還沒有教過它,但它很容易拾起。這裏的主要問題是如何將一個int分析到String.Format中,以便它提供所需的最大空間。使用String.Format動態提供所需填充的最大數量
這是循環:
for (int index = 0; index < files.Length; index++)
{
int maxNumericalSize = 2;
int fileNumberSystem = index + 1;
string result = string.Format("{0,maxNumericalSize}: {1,20} - {2,10} - {3}", fileNumberSystem, files[index].Name, files[index].Length, files[index].LastWriteTime);
/* Console.WriteLine(fileNumberSystem + ". " + files[index].Name + " - " + files[index].Length + " - " + files[index].LastWriteTime); */
Console.WriteLine(result);
}
正如你可以看到MaxNumericalSize明顯拋出一個領域,因爲它是語音引號內,所以我想目前我怎麼能解析到的String.Format一樣,如果沒有錯誤。
如果這是一個問題,您可以將'int'轉換爲'string'並填充它。如果你顯示當前的輸出結果並且預期明白你想要的結果會好得多(對不起,但我不能)。 – Sinatr
string result = string.Format(「{0,」+ maxNumericalSize +「}:{1,20} - {2,10} - {3}」,fileNumberSystem,files [index] .Name,files [index] .Length ,files [index] .LastWriteTime);應該管用 –