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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14051257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:31:50  来源:igfitidea点击:

Conversion from Int array to string array

c#.netarrayslinqlinq-to-objects

提问by InfantPro'Aravind'

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?

当我将整数数组转换为字符串数组时,我使用 for 循环以更长的方式执行此操作,如下面的示例代码中所述。这个有简写吗?

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

现有的问题和答案SO大约int[]string(不string[])。所以他们没有帮助。

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

虽然我发现这个Converting an int array to a String array答案但平台是 Java 而不是 C#。同样的方法无法实现!

        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();
        }

采纳答案by Tilak

int[] intarray = { 1, 2, 3, 4, 5 };
string[] result = intarray.Select(x=>x.ToString()).ToArray();

回答by Vishal Suthar

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();

回答by Rolwin Crasta

Try Array.ConvertAll

尝试 Array.ConvertAll

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

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