I have a list like so and I want to be able to search within this list for a substring coming from another string. Example:
List<string> list = new List<string>();
string srch = "There";
list.Add("1234 - Hello");
list.Add("4234 - There");
list.Add("2342 - World");
I want to search for "There"
within my list and return "4234 - There"
. I've tried:
var mySearch = list.FindAll(S => s.substring(srch));
foreach(var temp in mySearch)
{
string result = temp;
}
same problem i had to do.
You need this:
Where:
myList
isList<String>
has the values thatmyString
has to contain at any positionSo de facto you search if myString contains any of the entries from the list. Hope this is what you wanted...
i like to use indexOf or contains
What you've written causes the compile error
Substring is used to get part of string using character position and/or length of the resultant string.
for example
srch.Substring(1, 3)
returns the string "her"As other have mentioned you should use
Contains
which tells you if one string occurs within another. If you wanted to know the actual position you'd useIndexOf
And for
CaseSensitive
use:OR
To return all th entries:
Only the first one:
With Linq, just retrieving the first result:
To do this w/o Linq (e.g. for earlier .NET version such as .NET 2.0) you can use
List<T>
'sFindAll
method, which in this case would return all items in the list that contain the search term: