如何删除包含特定字符串的列表中的项目

时间:2017-01-05 06:47:28

标签: c# linq list

我有一个包含此数据的列表:"start it","start it now","don't start it" 我需要找到并删除包含"start it"的项目是否有一种简单的方法可以做到这一点?

6 个答案:

答案 0 :(得分:3)

如果要删除包含该子字符串"start it"的所有项目,则必须执行

List<string> items = new List<string>() { "start it", "start it now", "don't start it" };
items.RemoveAll(x => x.Contains("start it"));

如果您要删除所有等于"start it"的项目

items.RemoveAll(x => x == "start it");

答案 1 :(得分:2)

尝试list.RemoveAll(x=>x=="start it"); 其中listList<string>

答案 2 :(得分:0)

试试这个:

删除所有相等的字符串为&#34;启动它&#34;。

list.RemoveAll(x => x.Equals("start it"));

删除包含句子的所有字符串&#34;启动它&#34;。

list.RemoveAll(x => x.Contains("start it"));

答案 3 :(得分:0)

这可能会为你做到这一点

List<string> items = new List<string>() { "start it now", "start it", "don't start it" };
items.RemoveAll(x => x.Equals("start it"));
//or
items.RemoveAll(x => x == "start it");

ContainsEquals都在使用字符串比较。由于您的比较是string类型,Contains将检查传递的参数是否是字符串的一部分,而Equals比较完整的字符串是否相等。

答案 4 :(得分:0)

试试这个:

 string   mystring = "start it,start it now,don't start it";
 ArrayList strings = new ArrayList(mystring.Split(new char[] { ',' }));

 for (int i = 0; i <strings.Count; i++)
 {
     if (strings[i].ToString()=="start it")
     {
         strings.RemoveAt(i);
     }
 }

并且不要忘记: 使用System.Collections;

答案 5 :(得分:-2)

以下是您可以做的事情:

List<string> list = new List<string>() { "start it", "start it now", "don't start it" };
list.RemoveAll(x => x.Contains("start it"));