3

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.

1
  • Can you explain why you don't want to create a new array? Commented Feb 16, 2010 at 15:39

7 Answers 7

17

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();
Sign up to request clarification or add additional context in comments.

6 Comments

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.
Funny, this is the same answer as mine. I see what you did there!
@Codesleuth: Are you insinuating something?
@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: 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.
|
9

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

5 Comments

OK, so can we again copy back the newly created array in old one
@nagarjuna: Yes, if by "copy back" you mean set the reference of the old one to the new one.
no... except if you want it to have null at the end of the array
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.
@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).
2

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

Comments

1

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.

Comments

0

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.

Comments

0

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.

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.