32

When I am converting array of integers to array of string, I am doing it in a lengthier way using a for loop, like mentioned in sample code below. Is there a shorthand for this?

The existing question and answers in SO are about int[] to string (not string[]). So they weren't helpful.

While I found this Converting an int array to a String array answer but the platform is Java not C#. Same method can't be implemented!

        int[] intarray =  { 198, 200, 354, 14, 540 };
        Array.Sort(intarray);
        string[] stringarray = { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty};

        for (int i = 0; i < intarray.Length; i++)
        {
            stringarray[i] = intarray[i].ToString();
        }
4

3 回答 3

84
int[] intarray = { 1, 2, 3, 4, 5 };
string[] result = intarray.Select(x=>x.ToString()).ToArray();
于 2012-12-27T07:52:25.907 回答
14

试试 Array.ConvertAll

int[] myInts = { 1, 2, 3, 4, 5 };

string[] result = Array.ConvertAll(myInts, x=>x.ToString());
于 2014-10-30T08:58:39.833 回答
5

干得好:

林克版本:

String.Join(",", new List<int>(array).ConvertAll(i => i.ToString()).ToArray());

简单一:

string[] stringArray = intArray.Select(i => i.ToString()).ToArray();
于 2012-12-27T07:55:12.710 回答