A
A
Alexey Smirnov2016-12-22 14:52:23
.NET
Alexey Smirnov, 2016-12-22 14:52:23

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 nullor empty strings (string) "".
How to count the number of non-empty (not nulland not "") rows (rows) in this two-dimensional array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2016-12-22
@ERAFY

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();
    }

Read more: String.IsNullOrEmpty

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question