假设有一个list集合:
List<string> strList = new List<string>();
strList.Add("header");
strList.Add("founder");
strList.Add("tool" );
现在我想删除字符串为【header】
方法一:
for (int i = 0; i < strList.Count; i++)
{
if (strList[i]=="header")
{
strList.Remove(strList[i]);
i--;
}
}
重点是里面的i--,不然又会提示:集合已更改。
方法二(由qq_29814615提供!):
for (int i = strList.Count - 1; i > -1; i--)
{
if (strList[i] == "tool")
{
strList.Remove(strList[i]);
}
}