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
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))
Using LINQ
do
albumList.RemoveAll(x => cellVal[0].ToString().Equals(search)));
See this post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With