当您事先不知道数组中元素的数量时,是否可以格式化值数组。 我试过这个:
static void Main(string[] args)
{
object[] x = { 1, 2, 3 };
Console.WriteLine(string.Format("{0}", x));
Console.ReadKey();
}
这会产生“1”。
我正在尝试输出 1,2,3 或 "1","2","3"
最佳答案
使用string.Join
:
var result = string.Join(",", x); // 1,2,3
或:
var result = string.Join(",", x.Select(n => "\"" + n + "\"")); // "1","2","3"
引用 MSDN
关于c# - String.Format 格式化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317069/