0

I have array something like this

string[] arr = ['a', '', 'c', 'f', ''];

I want to remove null elements from the same.

How can I remove the required elements in C#?

6
  • 4
    none of those elements are null, or strings for that matter Commented May 20, 2014 at 7:07
  • 1
    stackoverflow.com/questions/8814811/… Commented May 20, 2014 at 7:08
  • stackoverflow.com/questions/14309072/… Commented May 20, 2014 at 7:08
  • 1
    Can you even have an empty char? Commented May 20, 2014 at 7:08
  • 3
    @RGraham Nope. char is a value type, so the closest you could do is the NUL character, \0. It's great when users post questions about code that won't even compile. Commented May 20, 2014 at 7:08

2 Answers 2

2

Use it as

arr = arr.Where(x => x != "").ToArray(); 
Sign up to request clarification or add additional context in comments.

Comments

2
string[] notNullStrings = arr.Where(it => !string.IsNullOrEmpty(it)).ToArray(); 

7 Comments

Those are not strings
@PatrickHofman oopss!
Regardless, it will remove strings that are null or empty which is what the question asked for, another improvement to your answer should be .Where(!string.IsNullOrEmpty) but i'm not at a computer where I can test this
@Sayse IMO string.IsNullOrEmpty() == false is easier to read than !string.IsNullOrEmpty()
It is less efficient though as it adds an extra comparison, although this may get handled by the compiler, it may not for some other compilers
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.