简体   繁体   English

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

[英]How to remove an item in a list that contains specific string

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

If you want to delete all items that contain that substring "start it" you have to do 如果要删除所有包含该子字符串的项目,请"start it"

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

if you want to remove all items that equal "start it" you have to 如果要删除所有等于"start it"项目, "start it"必须

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

try list.RemoveAll(x=>x=="start it"); 尝试list.RemoveAll(x=>x=="start it"); where list is your List<string> list是您的List<string>

Try this: 尝试这个:

It removes all the string thatis equal to "start it". 它删除所有等于 “ start it”的字符串。

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

It removes all the string that contains the sentence"start it". 它删除包含句子“ start it”的所有字符串。

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

This might do the trick for you 这可能为您解决问题

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");

Both, Contains and Equals are using string comparison. ContainsEquals都使用字符串比较。 Since your comparison is of type string, Contains will check if the passed parameter is part of the string, whereas Equals compares the complete string for equality. 由于您的比较是字符串类型,Contains将检查传递的参数是否为字符串的一部分,而Equals将比较整个字符串是否相等。

try this: 尝试这个:

 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);
     }
 }

and dont forget: using System.Collections; 并且不要忘记:使用System.Collections;

Here is what you can do: 您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM