我有字符串列表。如果列表包含該部分字符串,則找出該項目的索引。請查看代碼以獲取更多信息。使用linq獲取列表中部分匹配項目的索引
List<string> s = new List<string>();
s.Add("abcdefg");
s.Add("hijklm");
s.Add("nopqrs");
s.Add("tuvwxyz");
if(s.Any(l => l.Contains("jkl")))//check the partial string in the list
{
Console.Write("matched");
//here I want the index of the matched item.
//if we found the item I want to get the index of that item.
}
else
{
Console.Write("unmatched");
}
如果項目不在列表中,則導致異常。任何選擇? –
@SandeepKushwah:如果項目不在那裏,索引是-1,所以你只需要檢查。 –
感謝:) –