Pindo
Pindo

Reputation: 1625

remove all strings from list containing certain substring

I've got a list of strings called albumList the string has this structure: abc,abc,abc,abc,abc I ask the user to search an album to delete; if the album is found, it will be deleted from the list. There could be more than one album with the same name to be deleted from the list.

 private void searchAlbumName(string search)
        {
            string[] cellVal;
            foreach (string x in albumList)
            {
                cellVal = x.Split(',');
                if (cellVal[0].ToString().Equals(search))
                {
                    //do delete here
                }
            }
        }

I'm not sure how to remove every album with the search name

Upvotes: 6

Views: 9538

Answers (2)

bash.d
bash.d

Reputation: 13207

Using LINQ do

albumList.RemoveAll(x => cellVal[0].ToString().Equals(search)));

See this post.

Upvotes: 1

cuongle
cuongle

Reputation: 75306

You can use RemoveAll to get the result:

albumList.RemoveAll(x => x.Split(',')[0].ToString().Equals(search))

For easier to read, you can use:

albumList.RemoveAll(x => x.Split(',').First().Equals(search))

Upvotes: 11

Related Questions