Pregunta

I have a string array with 96 elements. I would like the array formatted into one string like this:

str    str
str    str
str    str
str    str

instead of like this:

str
str
str
str
str
str
str
str

but with 32 in each column instead of 4.

How can I do this?

Thanks.

¿Fue útil?

Solución

If you want 32 "rows" and 3 "columns" you can use this LINQ query:

string[] strings = Enumerable.Repeat("str", 96).ToArray();
IEnumerable<string[]> arrays = strings
    .Select((str, index) => new { str, index })
    .GroupBy(x => x.index / 3)
    .Select(g => g.Select(x => x.str).ToArray());

So each string[] contains three strings and the sequence contains 32 string[]s.

Otros consejos

int index = 0;
var result = String.Join(Environment.NewLine,
                         array.GroupBy(s => index++ / 3)
                              .Select(g => String.Join("\t", g)));

If you want strings to be aligned in columns, then instead of String.Join("\t", g) use:

String.Join("", g.Select(s => String.Format("{0,-20}",s)))

That will give each columns width of 20 characters and left-align strings in columns.

this may be the solution :

int[,] MyArray = new int[32,2];

now if you want to put values then :

MyArray[0,0] = your first value
//// until
MyArray[32,2] = your last value

surely you could use your desired type instead of the int type.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top