好这里的代码示例:什么是字符串格式C#{0,12:N0}(冒号和引号)
string header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
"City", "Year", "Population", "Change (%)");
Console.WriteLine(header);
string output;
foreach (var city in cities) {
output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
(city.Item5 - city.Item3)/ (double)city.Item3);
Console.WriteLine(output);
}
}
}
// The example displays the following output:
// City Year Population Year Population Change (%)
//
// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
// New York 1940 7,454,995 1950 7,891,957 5.9 %
// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
我了解的ARGS冒号后{0:N0}表示没有小数点,但昏迷呢?如{0,-12}和{1,12}逗号在字符串格式的参数后的含义是什么?
如果只有有[某种文件(https://msdn.microsoft.com/en-us/library/0c899ak8(V = vs.110)。 aspx) – Ian
@Ian:哦,谢谢!我只发现[这](https://msdn.microsoft.com/en-us/library/system.string.format%28v=vs.110%29.aspx)大声笑。谢谢,当我阅读msdn本身时,我并没有真正了解它,因为这个例子并不多。 –
花时间学习如何浏览MSDN页面。我个人发现,他们习惯了将他们与各种成员/方法区分开来,但一旦你习惯了他们,他们就非常棒。 – Ian