How to use C# convert 2 dimensional array string[,

2019-09-24 02:10发布

问题:

I have 2-d array

var temp = new string[,] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };

remind:

string[,] != string[][]

I want to convert to ...

123

456

789

How to fast convert in this case ?

回答1:

for a single code line, you can use:

var temp = new string[,] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } };
var result = string.Join("\r\n\r\n", 
    temp.OfType<string>()
    .Select((str, idx) => new {index = idx, value = str})
    .GroupBy(a => a.index/(temp.GetUpperBound(0) + 1))
    .Select(gr => gr.Select(n => n.value).ToArray())
    .Select(a => string.Join("", a.SelectMany(x => x)))
    .ToArray());

the single line code will look a lot nicer if you wont define the array as multidimensional array:

string[][] array2d = { new[] { "1", "2", "3" }, new[] { "4", "5", "6" }, new[] { "7", "8", "9" } };
string[][] jagged2d = { new[] { "1", "2", "3" }, new[] { "4", "5" }, new[] { "6" } };

string array2dConcatenate = string.Join("\r\n\r\n", array2d.Select(a => string.Join("", a.SelectMany(x => x))));
string jagged2dConcatenate = string.Join("\r\n\r\n", jagged2d.Select(a => string.Join("", a.SelectMany(x => x))));

to just concatenate a multi dimensional arrays you can use:

string[,] multidimensional2d = { { "1", "2", "3" }, { "4", "5", "6" } };
string[,,] multidimensional3d = { { { "1", "2" }, { "3", "4" } }, { { "5", "6" }, { null, null } } };
string multidimensional2dConcatenate = string.Join(", ", multidimensional2d.OfType<string>());
string multidimensional3dConcatenate = string.Join(", ", multidimensional3d.OfType<string>());

to make the best use of linq see: When to use Cast() and Oftype() in Linq

in case you want to use 3d array or protect from null you can do something like this:

string[][][] array3d = { new[] { new[] { "1", "2" } }, new[] { new[] { "3", "4" } }, new[] { new[] { "5", "6" } }, null };
string[][][] jagged3d = { new[] { new[] { "1", "2" }, new[] { "3" } }, new[] { new[] { "4" }, new[] { "5" } }, new[] { new[] { "6" }, null }, null };

string array3dConcatenate = string.Join("\r\n\r\n", array3d.Where(x => x != null).SelectMany(x => x).Where(x => x != null).Select(a => string.Join("", a.SelectMany(x => x))));
string jagged3dConcatenate = string.Join("\r\n\r\n", jagged3d.Where(x => x != null).SelectMany(x => x).Where(x => x != null).Select(a => string.Join("", a.SelectMany(x => x))));


回答2:

private static string ObjectToString(IList<object> messages)
    {
        StringBuilder builder = new StringBuilder();
        foreach (var item in messages)
        {

            if (builder.Length > 0)
                builder.Append(" ");
            if (item is IList<object>)
                builder.Append(ObjectToString((IList<object>)item));
            else
                builder.Append(item);

        }

        return builder.ToString();
    }


回答3:

Here is a way with 2 nested loops:

var temp = new string[,]{{"1","2","3"},{"4","5","6"}};
var output = new string[temp.GetUpperBound(0)+1];
for (int i = 0; i<=temp.GetUpperBound(0); i++)
{
    var sb = new StringBuilder(temp.GetUpperBound(1)+1);
    for (int j = 0; j<=temp.GetUpperBound(1); j++)
        sb.Append(temp[i,j]);
    output[i] = sb.ToString();
}

If you are thinking you can treat the 2-d array as 1-d and bypass the 2 loops there are some tricks here: How to copy a row of values from a 2D array into a 1D array? , but to be able to use these tricks you would need array of char, not array of string as you have asked.