简体   繁体   中英

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

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

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

try list.RemoveAll(x=>x=="start it"); where list is your List<string>

Try this:

It removes all the string thatis equal to "start it".

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

It removes all the string that contains the sentence"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. 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.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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