我有一个通用列表,其中包含一些字符串值。有时值会重复。而且我必须从列表集合中删除特定的值。我尝试了以下代码。
List<string> postalCodes = new List<string> { "A1B", "A2B", "A3B","A2B" };
string currentPostalCode = "A2B";
postalCodes.RemoveAt(postalCodes.IndexOf(currentPostalCode));
但是这段代码从位置1删除了项目,但从位置3删除了。如何从两个位置删除项目?请帮我 提前致谢。
postalCodes.RemoveAll(c => c == currentPostalCode);
如果要使用RemoveAt
,则必须循环执行:
int index;
while((index = postalCodes.IndexOf(currentPostalCode)) != -1)
{
postalCodes.RemoveAt(index);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句