Remove empty values ​​in an array using C #

Is there any method that removes empty indexes from an array for example

string[] test={"1","","2","","3"}; 

in this case, is there any method for removing an empty index from an array using C # at the end I want to get an array in this format test={"1","2","3"}; , which means that two indexes are removed from the array, and finally I got 3 indexes I am not the exact code for the array, this is the hint I want to do

+66
c #
Jan 11 2018-12-12T00:
source share
4 answers

If you are using .NET 3.5+, you can use linq.

test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();

+160
Jan 11 '12 at 5:59
source share
β€” -

You can use Linq if you are using .NET 3.5 or later:

  test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray(); 

If you cannot use Linq, you can do it like this:

 var temp = new List<string>(); foreach (var s in test) { if (!string.IsNullOrEmpty(s)) temp.Add(s); } test = temp.ToArray(); 
+29
Jan 11 '12 at 6:00
source share

I am writing the code below to remove an empty value in an array string.

 string[] test={"1","","2","","3"}; test= test.Except(new List<string> { string.Empty }).ToArray(); 
+2
Apr 25 '19 at 6:37
source share

I prefer to use two parameters: spaces and an empty field:

 test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray(); test = test.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); 
+1
Aug 24 '18 at 9:09
source share



All Articles