5

i have List<sting> with 5 entries. [0],[1],[2],[3],[4].

if I use List.Clear() all item are removed.

i need remove till specific item, for example till [1]. that means in my list are just 2 items [0] and [1]. how do that with c#?

braX
  • 11,506
  • 5
  • 20
  • 33
r.r
  • 7,023
  • 28
  • 87
  • 129

5 Answers5

8

If want to remove all items after index 1 (that is, retain only the first two items):

if (yourList.Count > 2)
    yourList.RemoveRange(2, yourList.Count - 2);

If you need to remove all items after the item with a value of "[1]", regardless of its index:

int index = yourList.FindIndex(x => x == "[1]");
if (index >= 0)
    yourList.RemoveRange(index + 1, yourList.Count - index - 1);
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • Don't you mean RemoveRange starting at 0 and removing `index` items? – Roman Jun 16 '11 at 14:10
  • @R0MANARMY: The question is ambiguous. The OP says "I need remove till specific item", but they also say "that means in my list are just 2 items [0] and [1]". You could be right, but I'll leave my answer as-is for now, until the OP clarifies their requirements. – LukeH Jun 16 '11 at 14:17
  • Could just be a cultural bias. We are used to a system where things go from left to right. For us those statements are inconsistent. But if you assume right to left and it means "I want to start on the right side of the list and remove items to the left until I get to the desired element." Then the two statements make sense. – Roman Jun 16 '11 at 14:25
4

You can use the GetRange method.

So..

myList = myList.GetRange(0,2);

..would give you what you are asking for above.

heisenberg
  • 9,665
  • 1
  • 30
  • 38
  • but i really need to delete all items which are after [1] – r.r Jun 16 '11 at 13:55
  • @Ragims The above code will return a list with just two items: `[0],[1]`. – Chris Shouts Jun 16 '11 at 13:57
  • @Ragims The new list only contains the 2 items [0] and [1], I don't really know what it is you are asking for since this seems to address your problem spot on as it is described in your post. – heisenberg Jun 16 '11 at 13:57
2

You can use the List.RemoveWhere(Predicate).. Alternatively, you can do a for loop - looping backwards, removing items till the item you're after ie

for(var i = List.Count()-1; i>=0; i--) {
   var item = List[i];
   if (item != "itemThatYourLookingFor") {
      List.Remove(item);
      continue;
   }
   break;
}
Jason Jong
  • 4,310
  • 2
  • 25
  • 33
0
List<string> strings = new List<string>{"a", "b", "c", "d", "e"};
List<string> firstTwoStrings = strings.Take(2).ToList();
// firstTwoStrings  contains {"a", "b"}

The Take(int count) method will leave you with count items.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
0

You can remove a range from a list, giving the starting index and the number of items to remove.

var items = new List<string> {"0", "1", "2", "3", "4", "5"};
var index = items.IndexOf("1") + 1;

if (index >= 0)
{
    items.RemoveRange(index, items.Count - index);
}
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • Probably worth noting that this will clear the entire list if the item isn't found. The OP doesn't specify what behaviour they want in that situation. (My answer leaves the list untouched if the item isn't found. Either option is equally (in)valid in the absence of any clarification from the OP.) – LukeH Jun 16 '11 at 15:24