Reputation: 4430
I have List<String>
include nine items.
I want to delete at item contains:
.
I do this with code:
foreach (string text in listHeader[0])
{
if (text.Contains(" "))
{
listHeader[0] = listHeader[0].SelectMany(line =>
{
line.Replace(" ", "");
return new String[] { line };
}).ToList();
}
}
But listHeader[0]
also have nine items. How to fix my code to delete item contains
.
Sample data:
listHeader[0]= {
text
sample
new string
}
Another case my listHeader[1]
need filter with start with <!--<div
to </div>-->
.
foreach (string text in listHeader[1])
{
listHeader[1] = Regex.Replace(text, "<!--<div.*?</div>-->", "").ToList();
}
My question is:
How to remove item contains string start with <!--<div
and end at </div>-->
Sample:
listHeader[1] = {
go to school
<!--<div id='fav_0118668891'class='icon displayOn' ><span class='favorite' onclick='javascript:addMatchFavorites("0118668891","014018",false,false);' title='undefined'></span></div>-->
work at the company
take a coffee
}
My resolve:
In this case:
I using StartWith
and EndWith
to find this string and Remove
.
listHeader[1].RemoveAll(x => x.StartsWith("<!--<div") && x.EndsWith("</div>-->"));
Upvotes: 1
Views: 70
Reputation: 16956
How to fix my code to delete item contains
You could use List.RemoveAll
method which removes all the elements that match the conditions defined by the specified predicate.
listHeader[0].RemoveAll(x=> x.Contains(" "));
Check this Demo
Upvotes: 3
Reputation: 27214
foreach (var item in listHeader[0])
{
if (item.Contains(" "))
nineItems.Remove(item); // remove item(s) containing  
}
OR a simpler way:
listHeader[0].RemoveAll(x => x.Contains(" "));
Upvotes: 3