Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list that contains {"12",
" ",
"13",
"10"
};
I would like to remove " " from my list, not just one at a time, but all at once, if you have more than one term " "

What I have tried:

// textInt_FNspt_N1 it is my textbox
List<string> N1 = new List<string>();
private static bool AcharOvazio(String s)
{
return s.ToLower().Contains("");
}

private void btn_FNspt_calnspt_Click(object sender, EventArgs e)
{
N1.Clear();

string[] ns = textInt_FNspt_N1.Text.Split('\n');

N1 = ns.ToList();
N1.RemoveAll(AcharOvazio);
}
Posted
Updated 9-Mar-20 21:32pm

Ehsan is on the right path, but it can be simplified (and corrected for syntax) to
C#
N1 = ns.Where(x => !String.IsNullOrWhiteSpace(x)).ToList();
 
Share this answer
 
v2
Comments
George Swan 10-Mar-20 4:35am    
shouldn't that be String.IsNullOrWhiteSpace?
OriginalGriff 10-Mar-20 4:50am    
Fixed - thanks!
In my world, whitespace is one word ... :laugh:
João Henrique Braga 10-Mar-20 13:32pm    
thanks!!!!!!!!!! <3
OriginalGriff 10-Mar-20 13:37pm    
You're welcome!
You need to use the method
C#
String.IsNullOrEmpty(string parameter)
like:

C#
private static bool AcharOvazio(String s)
{
  return String.IsNullOrEmpty(s.Trim());
}


we can also do it directly inline with Linq like:

C#
N1 = ns.Where(x => !String.IsNullOrEmpty(x.Trim()))ToList();
 
Share this answer
 
v2

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