Convert 2D array to string in C#, looking for most elegant way

10,736

Solution 1

Linq solution, not performance wise.

var str = string.Join(",", a.OfType<int>()
    .Select((value, index) => new {value, index})
    .GroupBy(x => x.index / a.GetLength(1))
    .Select(x => $"{{{string.Join(",", x.Select(y => y.value))}}}"));

Note that you cant Select on 2d array, but you can use OfType which will return an enumerable for 2d array, the enumerator will traverse through 2d array horizontally.

x.index / a.GetLength(1) simply divides each index to total number of rows. so if you have 3 rows, your indexes will be distributed through 3 rows equivalently.

lastly string join is operated on each group.


A little more simplified version. (format inside result selector of grouping)

var str = string.Join(",", a.OfType<int>()
    .Select((value, index) => new {value, index})
    .GroupBy(x => x.index / a.GetLength(1), x => x.value,
        (i, ints) => $"{{{string.Join(",", ints)}}}"));

Solution 2

AFAIK, when we want a string from an object, we are calling about serializing, So I prefer to use a serializer like Newtonsoft.Json:

var result = $@"{{{JsonConvert.SerializeObject(a)
    .Trim('[', ']').Replace("[", "{").Replace("]", "}")}}}";

A way by using simple fors like your solution and with removing ifs can be - this code will be faster for small arrays -:

var result = string.Empty;
var maxI = a.GetLength(0);
var maxJ = a.GetLength(1);
for (var i = 0; i < maxI; i++)
{
    result += ",{";
    for (var j = 0; j < maxJ; j++)
    {
        result += $"{a[i, j]},";
    }

    result += "}";
}

result = .Replace(",}", "}").Substring(1);

As suggested to use StringBuilder to improve performance just for big arrays:

var sb = new StringBuilder(string.Empty);
var maxI = a.GetLength(0);
var maxJ = a.GetLength(1);
for (var i = 0; i < maxI; i++)
{
    sb.Append(",{");
    for (var j = 0; j < maxJ; j++)
    {
        sb.Append($"{a[i, j]},");
    }

    sb.Append("}");
}

sb.Replace(",}", "}").Remove(0, 1);
var result = sb.ToString();

Solution 3

Consider this approach:

var numbers = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

var results = string.Join(",",
    Enumerable.Range(0, numbers.GetUpperBound(0) + 1)
        .Select(x => Enumerable.Range(0, numbers.GetUpperBound(1) + 1)
            .Select(y => numbers[x, y]))
        .Select(z => "{" + string.Join(",", z) + "}"));

Console.WriteLine(results);

Console.ReadLine();

It is quite similar to yours, but using LINQ instead. It projects the two dimensional array into a LINQ enumerable, and then wraps it with braces and adds commas where needed.

Share:
10,736
maf-soft
Author by

maf-soft

Updated on June 04, 2022

Comments

  • maf-soft
    maf-soft almost 2 years

    I cannot believe there is no smart way to get something like this from a 2D array,
    in this case int[,] a:

    "{1,2,3},{4,5,6},{7,8,9}"
    

    I have read many similar questions and learned that string.Join() can only be used on jagged arrays (in 2D). But I don't want to use them because of the more complex initialization and because it just feels bad when my rows, which all have the same length, are spread over several places in memory.

    This is my "normal" code:

    var s = "";
    for (int i = 0; i < a.GetLength(0); i++) {
        if (i > 0) s += ',';
        s += '{';
        for (int j = 0; j < a.GetLength(1); j++) {
            if (j > 0) s += ',';
            s += a[i, j];
        }
        s += '}';
    }
    

    And here a "golfed" one:

    var s = "{";
    var i = 0;
    foreach (var item in a) s += (i++ > 0 ? i % a.GetLength(1) == 1 ? "},{" : "," : "") + item;
    s += '}';
    

    :) - not really elegant, too, and the readability is more than bad.

    Any suggestions? I'm open to Linq, since it doesn't have to be fast. I'm interested in improving elegance of the code, but not by just moving it to an extension method.

  • maf-soft
    maf-soft over 4 years
    This does not meet the requirements or you have interpreted them too generously. It wasn't about being able to parse it back, it was about presenting the data in a structured way.