Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a String Item from a List of Strings

How do I remove a specific string from a List that contains Strings....

As in:

ArrayList<String> myStrings = new ArrayList<>();
myStrings.add("Alpha");
myStrings.add("Beta");
myStrings.add("Gama");
.                           //The order can be random 
.
.
.

Now , I only have the list myStrings and I don't know which String is at which index. But I know, that I want to display all the strings after removing say "Alpha".

To Summarize , How can I get the strings from a String array after removing a String that I know that array contains , but don't know its index/position.

like image 537
asdec90 Avatar asked Apr 02 '15 07:04

asdec90


2 Answers

Use remove :

myStrings.remove("Alpha");

Note that this would only remove the first occurrence of "Alpha" from your list.

like image 63
Eran Avatar answered Oct 31 '22 08:10

Eran


if this not work

myStrings.remove("Alpha");

try this it works fine with me

int pos = myStrings.indexOf("Alpha");//Getting string position
myStrings.remove(pos);
like image 24
Elfnan Sherif Avatar answered Oct 31 '22 06:10

Elfnan Sherif