简体   繁体   中英

Convert multi-dimension array to string

I have a 3 dimensional char array initialized as such:

char[,,] cube = new char[10, 10, 10];

It's completely filled and I want to convert its contents to a single string. My current method is this:

for (int z = 0; z < 10; z++) {
    for (int y = 0; y < 10; y++) {
        for (int x = 0; x < 10; x++) {
            build += cube[z, y, x];
        }
    }
}

Attempting to do build = new string(cube) gives an error:
cannot convert from 'char[*,*,*]' to 'char*'

The for loops are incredibly fast, completing in less than a millisecond on my setup (from 1500 to 4000 ticks). Wondering if a single line method exists that will accomplish the same thing that these nested for loops are doing?

EDIT:
This code will only be used once in the entire program, so I don't need something reusable.

LINQ is your friend.

A multidimensional array implements IEnumerable but sadly apparently not IEnumerable<T> . So first we need to get an IEnumerable<T> to be able to make full use of LINQ. Luckily, we know in this case, that every item in that multidimensional array is of type char , we just need to tell that to the compiler.

Next, to create a string , there is a convenient constructor, that accepts a char[] . And getting a char[] from an IEnumerable<char> is just one ToArray() away.

Put that together and you get:

using System.Linq;

var build = new string(cube.OfType<char>().ToArray());

This is easier than you think:

public static String FlattenToString(this char[,,] array)
{
    var builder = new StringBuilder();

    foreach(var @char in array)
    {
        builder.Append(@char);
    }

    return builder.ToString();
}

var cube = new char[2,2,2];

cube[0,0,0] = 'a';
cube[0,0,1] = 'b';
cube[0,1,0] = 'c';
cube[0,1,1] = 'd';
cube[1,0,0] = 'e';
cube[1,0,1] = 'f';
cube[1,1,0] = 'g';
cube[1,1,1] = 'h';

Console.WriteLine(cube.FlattenToString());

Prints out abcdefgh .

string build = string.Concat(cube.Cast<char>());

Probably not needed in your case, but a much faster alternative is copying to char[] :

//var cube = new[, ,] { { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } };
char[] temp = new char[cube.Length];
Buffer.BlockCopy(cube, 0, temp, 0, temp.Length * sizeof(char));
string build = new string(temp);
char[,,] cube = new char[10, 10, 10];
                for (int z = 0; z < 10; z++)
                {
                    for (int y = 0; y < 10; y++)
                    {
                        for (int x = 0; x < 10; x++)
                        {
                            cube[z, y, x] = (char)(65+x);
                        }
                    }
                }
/* Just Filling data in array*/    
                var s1 = cube.OfType<char>().ToList();
                string s = string.Join("",s1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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