Question

How to remove Null values in string array

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

I need to remove those null values in between and re-size the array

  1. I don't want to use lists

  2. I don't want to create a new array

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

Was it helpful?

Solution

No, it's not possible without creating a new array. You can't resize an array.

You can easily create a new array without empty strings and null references like this:

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

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

OTHER TIPS

If you don't want to create a new array, then no, it's not possible. You cannot add or remove an item from a simple array (as in, string[]).

The most straightforward way to accomplish what you want to achieve (if you remove your second requirement) would be:

  1. Count the number of null values in your source array
  2. Create a new array of the same length as your source array minus the number of nulls from step 1
  3. Copy all non-null values from your source array into the new array
  4. (Optional) Set the reference to your source array (e.g., srcArray) to your new array

As Dan said, you can't add or remove values from an Array. You can, however, use LINQ to remove the values and produce a second array.

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

Probably not the most performant solution but...

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

It will create a new array, but I cannot think of a solution that won't.

Before deciding how to proceed, you really need to think about who holds a reference to the array you are operating on.

If the array is not referenced by any other code (as a member of a class, as a captured variable in a lambda, or in some collection somewhere) then you shouldn't worry about creating a new array. In that case I would use something like what @Codesleuth or @Guffa suggest.

However, if other code may exist that holds a reference to this same array - then you are out of luck, unless you can safely identify and update the references held in those other places. This is a hard thing to do - and you should be very careful assuming that you can always update all other places where a reference is held.

Am I the only one here that would scan the array and move the members back over the NULLs, therefore making a continuous list of non-nulls.

This doesn't create a new array and it's simple to implement and it's immportant to know you can move the entries around the array.

Unfortunately I'm at work so can not supply full code, however you would implement it by searching the array for NULLs then moving the remaining items in the array up one. Keep doing this until the end. I would suggest clearing the remaining entires once the search is completed.

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

// select non-null elements only
_array= _array.Where(a => !String.IsNullOrEmpty(a)).ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top