Answer the question
In order to leave comments, you need to log in
How to count the number of non-empty rows (rows) in a two-dimensional array?
Hello.
There is a two-dimensional array with string (string) data. At the end of this two-dimensional array there are rows (rows) with elements containing either null
or empty strings (string) ""
.
How to count the number of non-empty (not null
and not ""
) rows (rows) in this two-dimensional array?
Answer the question
In order to leave comments, you need to log in
I didn't really understand where it is, "at the end of the array"
But, to count all non-empty (not null and not ""), I would go something like this
static void Main(string[] args)
{
string[,] Arr = new string[4, 2] { { "one", "two" }, { null, "" }, { "three", "four" },
{ "five", "six" } };
//счетчик
int count = 0;
//для каждой строки массива
for (int i = 0; i < Arr.GetLength(0); i++)
{
//проходим по каждому элементу текущей строки
for (int j = 0; j < Arr.GetLength(1); j++)
{
//если не null и не пустая, инкрементируем
if (!String.IsNullOrEmpty(Arr[i, j])) count += 1;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question