使用for删除列表中的节点

Coo*_*per 0 c# split list

这是我做的:

// this is an example of my function, the string and the remover should be variables
string delimeter = ",";
string remover="4";
string[] separator = new string[] { "," };
List<String> List = "1,2,3,4,5,6".Split(separator, StringSplitOptions.None).ToList();
for (int i = 0; i < List.Count - 1; i++)
{
    if(List[i]==remover)
        List.RemoveAt(i);
}
string allStrings = (List.Aggregate((i, j) => i + delimeter + j));
return allStrings;
Run Code Online (Sandbox Code Playgroud)

问题是重新编译的字符串与原始字符串相同,与"1,2,3,4,5,6"相同."4"仍在其中.

怎么解决?

编辑:

解决方案是我没有检查列表中的最后一个节点,因为它在示例中看起来不像是因为它是我刚才给出的一个例子

Bra*_*ner 5

当你从这样的列表中删除项目时,你应该让你的for循环反向运行,从最高索引到最低.

如果你从最低到最高,你将最终移除项目,因为它们被删除,这将跳过项目.反向运行它没有这个问题.