views:

163

answers:

7

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.

+9  A: 

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
Dan Tao
OK, so can we again copy back the newly created array in old one
Nani
@nagarjuna: Yes, if by "copy back" you mean set the *reference* of the old one to the new one.
Dan Tao
no... except if you want it to have null at the end of the array
nanda
I got the first 3 points. How to create reference. If we do so the length of the source array will change? I mean Will it get re ordered.
Nani
@nagarjuna: What I mean is this: say you have a method that performs steps 1-3 above called `RemoveNullStrings` and this returns a `string[]` (the new array). Then if you have a source array, `arr`, with nulls in it, you can write: `arr = RemoveNullStrings(arr);` and `arr` will then be set to the new array returned by the method (with nulls removed and therefore a smaller length).
Dan Tao
A: 

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()
Codesleuth
+9  A: 

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();
Guffa
`s` is a range variable that represents each string that is already in the array. Essentially it's just filtering out all null-or-empty strings and converting the resultant `IEnumerable<string>` to an array.
Will Vousden
Thank You. It worked exactly.
Nani
Funny, this is the same answer as mine. I see what you did there!
Codesleuth
@Codesleuth: Are you insinuating something?
Guffa
@Guffa: I'm directly saying that is the same answer as mine, albeit with more commentary. I didn't realise I was being vague, sorry.
Codesleuth
@Codesleuth: You are still being quite vague. Usually when someone says something like that, they want to say something more than just pointing out the similarities.
Guffa
@Guffa: I didn't intend for the comment to be directed at you. I was commenting that our answers are the same, which is fine, and it happens a lot when they are posted so soon after each other. I was exclaiming my despair at how technically my answer was posted first, and yet you got the tick. I don't know what you thought I meant; perhaps you should say.
Codesleuth
+1  A: 

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.

Mikael Sundberg
A: 

It is actually possible to remove the elements without creating a new array. This is the code to do it:

IEnumerable<string> items = originalArray.Where(s => s != null);

While all other answers use ToArray(), I simply left that out of this answer.

Steven
No Steven, It's not working.
Nani
@nagarjuna: This solution doesn't fulfill all your requirements (you're talking about 'resizing an array without creating a new one' which is impossible, as others already explained), but this code simply creates a view on top of the original array, without altering the original array. It saves your from recreating the array. In what way this solution is not working for you?
Steven
A: 

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.

LBushkin
A: 

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.

Neil