Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 710
maf-soft Avatar asked Feb 03 '26 22:02

maf-soft


2 Answers

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();
like image 158
shA.t Avatar answered Feb 05 '26 12:02

shA.t


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)}}}"));
like image 44
M.kazem Akhgary Avatar answered Feb 05 '26 11:02

M.kazem Akhgary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!