T K

T K

  • NA
  • 31
  • 6.3k

Compare sub strings in a List of strings

Jun 17 2015 12:51 AM
I have declared following List of strings-
List<string> lstfruits= new List<string>(){
{ "apple-2015.05.18.html" },
{ "apple-1997.01.11.html" },
{ "mango-2013.02.12.html" },
{ "peach-2015.04.12.html" },
{ "peach-1998.11.12.html" },
{ "other-2013.02.12.html" },
};
I need to match all records which contain 'apple'. Below are the results that I want-
"apple-2015.05.18.html"
"apple-1997.01.11.html"
I wrote below , but it returns zero results-
var items = lstfruits.Where(pp => pp.Substring(0, pp.IndexOf('-')) == "apple");
Please help where I am wrong.

Answers (8)