I want to remove them all but it's removing only some of
the "" and leave some empty strings in the list.
You should have shown an example of the string being spilt.
That might help to clarify why you are splitting on a double
comma substring.
There is a fundamental flaw in your logic when removing
elements (strings) from a List. Your code fails to
recognize that when you remove an item from a List
then all remaining items below the one removed
move up in the List. So their original index positions
change whenever an element is removed from the List.
So if there are two consecutive empty lines in the
List and you remove the first one, then the second one
will move up and occupy the position (index) that the
first one previously occupied. But your code moves to
the next index and thus never checks the string that
just moved up in the List.
One method to allow for this is to remove items from
the List starting at the bottom and moving up towards
the top of List.
for (int i = test.Count - 1; i >= 0; --i)
{
if (test[i] == "")
{
test.RemoveAt(i);
}
}
Or compensate for the shift by retesting the string at the
same index as the one just removed. As in Viorel's last
example or like this:
for (int i = 0; i < test.Count; i++)
{
if (test[i] == "")
{
//test.RemoveAt(i);
test.RemoveAt(i--);
}
}