简体   繁体   English

如何在 C# 中使用 string.Join 以 [1, 2, 3, 4 ,5] 格式打印数组?

[英]How to print array in format [1, 2, 3, 4 ,5] with string.Join in C#?

i have an array 1 2 3 4 5 in C#.我在 C# 中有一个数组1 2 3 4 5 I just want to print in format with: [] ;我只想用以下格式打印: [] ; Ex.前任。 [1, 2, 3, 4, 5]

You can use string.Format and string.Join combined您可以将string.Formatstring.Join结合使用

var output = string.Format("[{0}]", string.Join(",", yourArray));

and then you just need to print output string anywhere you want.然后你只需要在你想要的任何地方打印output字符串。

String.Format will provide you with possibility to wrap joined string with [ and ] without concatenating string manually. String.Format将为您提供用[]包装连接字符串的可能性,而无需手动连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM