简体   繁体   English

从 Int 数组到字符串数组的转换

[英]Conversion from Int array to string array

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.当我将整数数组转换为字符串数组时,我使用 for 循环以更长的方式执行此操作,如下面的示例代码中所述。 Is there a shorthand for this?这个有简写吗?

The existing question and answers in SO are about int[] to string (not string[] ). SO 中现有的问题和答案是关于int[]string (不是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#.虽然我发现这个Converting an int array to a String array答案但平台是 Java 而不是 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();
        }
int[] intarray = { 1, 2, 3, 4, 5 };
string[] result = intarray.Select(x=>x.ToString()).ToArray();

Try Array.ConvertAll尝试 Array.ConvertAll

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

string[] result = Array.ConvertAll(myInts, x=>x.ToString());

Here you go:干得好:

Linq version:林克版本:

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

Simple one:简单一:

string[] stringArray = intArray.Select(i => i.ToString()).ToArray();

I need the same thing as mentioned:我需要与提到的相同的东西:

string[] stringArray = intArray.Select(i => i.ToString()).ToArray();

but for multidimensinal array.但对于多维数组。 Let's say I have假设我有

int[,] intArray = new int[4, 4];

and I want to pass it to a function that requires string multidimensional array, so I want to convert it to string when I call that function.我想将它传递给一个需要字符串多维数组的函数,所以我想在调用该函数时将其转换为字符串。

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.当我将整数数组转换为字符串数组时,我使用for循环以更长的方式执行此操作,如下面的示例代码中所述。 Is there a shorthand for this?有速记吗?

The existing question and answers in SO are about int[] to string (not string[] ). SO中现有的问题和答案是关于string int[] (不是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#.虽然我发现此将int数组转换为String数组的答案,但平台是Java而不是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();
        }

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

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