NULL in an array of strings

How to remove null values โ€‹โ€‹in an array of strings

Like {, -2,3, -4, + 5, 66 ...}

I need to remove these null values โ€‹โ€‹between them and resize the array

  • I do not want to use lists

  • I do not want to create a new array

Please let me know if this is possible with simple code. Thank.

+3
source share
7 answers

No, this is not possible without creating a new array. You cannot resize an array.

You can easily create a new array without empty lines and null references, for example:

string[] items = new string[] { "", "-2", "3", null, "-4", "+5", null, "66" };

items = items.Where(s => !String.IsNullOrEmpty(s)).ToArray();
+16
source

, , . (, string[]).

, ( ), :

  • , , 1
  • () (, srcArray)
+9

, Array. LINQ .

originalArray = originalArray.Where(s => !string.IsNullOrEmpty(s)).ToArray()
+2

, , ...

array.Where(s => s != null).ToArray();

, , .

+1

, , , , .

- ( , - - ) , . - @Codesleuth @Guffa.

, , - , , . , - , , , .

0

, NULL, .

, , .

Unfortunately, Iโ€™m at work, therefore I canโ€™t provide the full code, however you would implement it by searching the array for NULL and then moving the remaining elements up to the array. Keep doing this until the end. I would suggest clearing the remaining items after completing the search.

0
source
string[] _array= new string[] { "", "z", "d", null, "a", "b", null, "66" };

// select non-null elements only
_array= _array.Where(a => !String.IsNullOrEmpty(a)).ToArray();
-1
source

Source: https://habr.com/ru/post/1733006/


All Articles