I have a list of Strings.
How can I check if the list of Strings contains a string with the prefix 2#?
I saw this:
Find substring in a list of strings
I tried the following:
if ((startId == item.ID.ToString())
&& (item.NextButtons.Contains(s => s.Contains("2#")))) ;
{
OK = false;
}
If I understood correctly:
Use the LINQ
Anymethod to determine if any element of a collections satisfies the condition.The condition is a lambda method that takes each
stringelement of your list as an input parameter and checks if itContainsthe string"2#". It does this until the first one is found and then returns eithertrueorfalse.If you wanted to return the actual string, you would use the
FirstOrDefaultmethod instead ofAny. It would then return either the string ornull, if none are found.edit: if the
2#has to be at the start of the string, see @Some1.Kill.The.DJ’s answer.