Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all strings from list containing certain substring

Tags:

c#

list

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

like image 433
Pindo Avatar asked Apr 01 '13 17:04

Pindo


2 Answers

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))
like image 170
cuongle Avatar answered Sep 28 '22 08:09

cuongle


Using LINQ do

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

See this post.

like image 35
bash.d Avatar answered Sep 28 '22 06:09

bash.d