Consider 2D array like this one:
With the following code the empty rows are skipped:
public static string[,] SkipBlankRows(this string[,] array2D)
{
var columns = array2D.GetLength(1);
var rows = array2D.GetLength(0);
var temp = new List<string[]>();
for (var r = 0; r < rows; r++)
{
var row = new string[columns];
for (var c = 0; c < columns; c++)
{
row[c] = array2D[r, c];
}
if (row.All(itm => string.IsNullOrWhiteSpace(itm)))
continue;
temp.Add(row);
}
string[,] result = new string[temp.Count(), columns];
rows = temp.Count();
for (int r = 0; r < rows; r++)
{
var row = temp[r];
for (var c = 0; c < row.Length; c++)
{
result[r,c]=row[c];
}
}
return result;
}
Usage:
void Main()
{
var x = new string[,] { { "", "", "" }, { "", "X", "" }, { "X", "X", "X" }, { "", "", "" }, {"X","","X"}, {"X","X","X"}};
var y = x.SkipBlankRows();
}
Result:
The result should be 2D-array of string
where the blank rows will not be there.
The code looks awkward to me, is it possible to do it better e.g. to involve linq?