我有一个List<string>
我检查它是否包含一个字符串:
if(list.Contains(tbItem.Text))
如果这是真的我会这样做:
int idx = list.IndexOf(tbItem.Text)
但是,如果我有 2 个相同的字符串怎么办?我想获取包含该字符串的所有索引,然后使用 foreach 循环遍历它。我怎样才能做到这一点?
假设list is a List<string>
:
IEnumerable<int> allIndices = list.Select((s, i) => new { Str = s, Index = i })
.Where(x => x.Str == tbItem.Text)
.Select(x => x.Index);
foreach(int matchingIndex in allIndices)
{
// ....
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)