在 C# 中将 Object[] 数组转换为 string[] 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15318946/
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 16:24:42  来源:igfitidea点击:

Converting a Object[] array to an string[] array in c#?

c#arrays

提问by JerryCai

I have many ways to do it but still want to know is there any offical API to convert object[] to string[] With null checked? Thanks

我有很多方法可以做到这一点,但仍然想知道是否有任何官方 API 可以将 object[] 转换为 string[] 并检查空值?谢谢

采纳答案by dtb

If you have a method to convert an objectto a string(or int) with null check, then you can use the Array.ConvertAll<TInput, TOutput> Methodto convert an object[]to a string[](or int[]):

如果您有将 an 转换object为 a string(或int)并带有空检查的方法,那么您可以使用Array.ConvertAll<TInput, TOutput> 方法将 an 转换object[]为 a string[](或int[]):

object[] input = ...;
string[] result = Array.ConvertAll<object, string>(input, ConvertObjectToString);
string ConvertObjectToString(object obj)
{
    return obj?.ToString() ?? string.Empty;
}

If you want to skip items in the object[]array when converting to a string[], a more convenient approach might be using the extension methods of the Enumerable Class:

如果您想object[]在转换为 a 时跳过数组中的项目string[],更方便的方法可能是使用Enumerable Class的扩展方法:

object[] input = ...;
string[] result = input.Where(x => x != null)
                       .Select(x => x.ToString())
                       .ToArray();

回答by Kaiser12

You could just run a simple conversion on it using the ConvertAll method in the Array class and a lambda on the object itself.

您可以使用 Array 类中的 ConvertAll 方法和对象本身的 lambda 对其运行简单的转换。

object[] inputArray = new object[10];
string[] resultArray = Array.ConvertAll(inputArray, x => x.ToString());

回答by Daniel Imms

Here is an extension method that allows the choice of including/excluding nulls, and what value to use to replace null if included.

这是一个扩展方法,允许选择包含/排除空值,以及使用什么值替换空值(如果包含)。

public static class ObjectArrayExtensions
{
    public static string[] ToStringArray(this object[] array, bool includeNulls = false, string nullValue = "")
    {
        IEnumerable<object> enumerable = array;
        if (!includeNulls)
            enumerable = enumerable.Where(e => e != null);
        return enumerable.Select(e => (e ?? nullValue).ToString()).ToArray();
    }
}

Usage:

用法:

object[] array = new object[] { 10, "hello", 5, "world", null };

var stringArray = array.ToStringArray();
// stringArray = { "10", "hello", "5", "world" }

stringArray = array.ToStringArray(true);
// stringArray = { "10", "hello", "5", "world", "" }

stringArray = array.ToStringArray(true, "empty");
// stringArray = { "10", "hello", "5", "world", "empty" }

Thanks to 280Z28 for improving the answer.

感谢 280Z28 改进答案。

回答by ja72

Try this:

尝试这个:

object[] input = {1, 2, null, 4, 5, 0.438, 7};
int[] result = input.OfType<int>().Select((o) => (int)o).ToArray();
// {1, 2, 4, 5, 7}

Or alternative going through stringfirst:

或者string首先通过的替代方法:

object[] input = {1, 2, null, 4, 5, 6.438, 7};
int[] result = input.Where((o) => o != null)
    .Select((o) =>
    {
        string s = o.ToString();
        int x = 0;
        int.TryParse(s, out x);
        return x;
    }).ToArray();
// { 1, 2, 4, 5, 0, 7}