Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Remove an item from a List<string>

How to: Remove an item from a List

I have got the following code snippet...

companies.Remove(listView_Test.SelectedItem.ToString());

There is a listView that contains (let's say) 3 items without a name, just with a Content of "A", "B" and "C". Now when I select an item of that listView, I secondly click on a button, which runs my method containing Remove()/RemoveAt(). Now I want to delete the line of the List<string> myList where the line is same to the Content of the selected item.

Edit: Solution by Flow Flow OverFlow

int index = companies.IndexOf(companyContent);
companies.RemoveAt(index);
like image 634
sjantke Avatar asked May 30 '14 11:05

sjantke


People also ask

How do I remove an item from a string in Python list?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.


1 Answers

You have to get the index of the object you wanna remove from the list, then you can:

//Assuming companies is a list 
companies.RemoveAt(i);

To get the index of the item you can use :

companies.IndexOf("Item");

or use a for loop with conditional statements:

for (int i = 0; i < companies.Count; i++) {
   // if it is List<String>
   if (companies[i].equals("Something")) {
         companies.RemoveAt(i);   
   }
}
like image 125
Transcendent Avatar answered Nov 14 '22 20:11

Transcendent