Marilou Marilou - 1 year ago 64
C# Question

How can I join an array of strings but first remove the elements of the array that are empty?

I am using the following:

return string.Join("\n", parts);

Parts has 7 entries but two of them are the empty string "". How can I first remove these two entries and then join the remaining five?

Answer Source

You can use Where in LINQ to select strings which are not empty:

 return string.Join("\n", parts.Where(s => !string.IsNullOrEmpty(s)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download