Why when removing empty strings from a list it's not removing all ther empty strings ?

sharon glipman 441 Reputation points
2021-08-28T20:40:37.767+00:00
List<string> test = word.Split(new string[] { ",," }, StringSplitOptions.None).ToList();

                for(int i = 0; i < test.Count; i++)
                {
                    if (test[i] == "")
                    {
                        test.RemoveAt(i);
                    }
                }

After the loop still some items are ""

But I want to remove them all but it's removing only some of the "" and leave some empty strings in the list.

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-08-28T21:21:45.223+00:00

    Try another parameter:

    List<string> test = word.Split( new string[] { ",," }, StringSplitOptions.RemoveEmptyEntries ).ToList();
    

    The loop is not needed. But if you have other lists, then:

    test.RemoveAll( s => s.Length == 0 ) ;
    

    or:

    for( int i = 0; i < test.Count; )
    {
       if( test[i] == "" )
       {
          test.RemoveAt( i );
       }
       else
       {
          ++i;
       }
    }
    
    0 comments No comments

  2. WayneAKing 4,931 Reputation points
    2021-08-28T22:19:59.503+00:00

    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--);
        }
    }
    
    • Wayne
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.