Click here to Skip to main content
15,885,980 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
suppose i have an array like
string[] test={"1","","2","","3"};

i want to remove it so the answer is like
test={"1","2","3"}

but without using lambda expression like
test =  test.Where(x => !string.IsNullOrEmpty(x)).ToArray();


How to achieve this?




Thanks in advance

Note : Cannot use lambda expression because it blocks changes at time of debugging
Posted
Updated 17-Apr-14 18:33pm
v2

C#
string[] test = { "1", "", "2", "", "3" };
test = test.Except(new List<string> { string.Empty }).ToArray();
 
Share this answer
 
v2
Hi,
You can use foreach loop instead of lamda expression.

C#
string[] test1;
int j=0;
foreach(item i in test)
{
if(!string.IsNullOrEmpty(i))
{
j=j+1;
test1[j] = i
}
}



I have not tested code but you can do something like this.
 
Share this answer
 
Comments
agent_kruger 18-Apr-14 5:00am    
vote 1, because how it is deleting the element?
Bh@gyesh 18-Apr-14 5:10am    
Hi,
see final result you want as an array with not null and empty records. Now if you delete element from an array using forloop, so it will give error as you can't change collection runtime.
So I suggested another array which will store your required result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900