开发者

Convert a list<int> to a joined string of ints?

I have an int array with the value 3,99,6. How do i convert the array into开发者_高级运维 the string 3,99,6 with linq?


int[] list = new [] {3, 99, 6};
string s = string.Join(",", list.Select(x => x.ToString()).ToArray());

Edit, C# 4.0

With C# 4.0, there is another overload of string.Join, which finally allows passing an IEnumerable<string> or IEnumerable<T> directly. There is no need to create an Array, and there is also no need to call ToString(), which is called implicitly:

string s = string.Join(",", list);

With explicit formatting to string:

string s = string.Join(",", list.Select(x => x.ToString(/*...*/));


Stefan's solution is correct, and pretty much required for .NET 3.5. In .NET 4, there's an overload of String.Join which takes an IEnumerable<string> so you can use:

string s = string.Join(",", list.Select(x => x.ToString());

or even just:

string s = string.Join(",", list);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜