的string.join一个列表&LT; INT&GT;或其它类型的 [英] string.Join on a List<int> or other type
本文介绍了的string.join一个列表&LT; INT&GT;或其它类型的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想打开int数组或列表到逗号分隔的字符串,如:
I want to turn an array or list of ints into a comma delimited string, like this:
string myFunction(List<int> a) {
return string.Join(",", a);
}
不过的string.join只需要列表&LT;串GT;
作为第二个参数。什么是做到这一点的最好方法是什么?
But string.Join only takes List<string>
as the second parameter. What is the best way to do this?
推荐答案
最好的办法是升级到.NET 4.0,其中有一个重载,你想要做什么:
The best way is to upgrade to .NET 4.0 where there is an overload that does what you want:
如果您不能升级,您可以使用选择并ToArray的达到同样的效果。
If you can't upgrade, you can achieve the same effect using Select and ToArray.
return string.Join(",", a.Select(x => x.ToString()).ToArray());
这篇关于的string.join一个列表&LT; INT&GT;或其它类型的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文