Convert a list to a joined string of ints?

后端 未结 2 949
猫巷女王i
猫巷女王i 2020-12-16 14:51

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?

2条回答
  •  情书的邮戳
    2020-12-16 15:10

    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 or IEnumerable 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(/*...*/));
    

提交回复
热议问题