Question by
salamander555 · Feb 04, 2017 at 03:19 PM ·
liststringindexindexof
Finding all the same strings in a List
Hello
So i need to find all string with the same in a List. i don't need the name of the string but i need the index of it. and now if want to find the index it keep giving me the same index.
you need further information ask below.
Comment
Answer by Alanisaac · Feb 04, 2017 at 03:48 PM
LINQ to the rescue! From http://stackoverflow.com/questions/13055391/get-indexes-of-all-matching-values-from-list-using-linq
var list = new List<string>
{
"Red",
"Blue",
"Green",
"Red",
"Red",
"Green"
};
var indexes = Enumerable.Range(0, list.Count).Where(i => list[i] == "Red").ToList(); // {0, 3, 4}
Answer by IgorAherne · Feb 05, 2017 at 11:30 PM
Aditionally, if you need just one value, use
myAbils.IndexOf("string to find"); //returns -1 if not found