How to find a string in a string that is in a List?

44 Views Asked by At

I've seen problems similar to mine and I found the solution, however my problem is a little complex. I want to find a specific string in the list. This my list:

List = ['JFK,John F Kennedy International,5326,5486', 'ORY,Paris-Orly,629,379', 'MAD,Adolfo Suarez Madrid-Barajas,1428,1151', 'AMS,Amsterdam Schiphol,526,489', 'CAI,Cairo International,3779,3584']

I want to be able to find 'JFK' or 'ORY' or 'MAD' or 'AMS' or 'CAI' or anything.

If you want to split the list that's also fine.

However then I would also like to be able to print that whole line where the three letter code came from. For example, if the the three letters found is 'AMS', I would later like to print 'CAI,Cairo International,3779,3584'

1

There are 1 best solutions below

0
On

I my friend, I hope you are well.

  List<string> airports = new List<string>() {"JFK,John F Kennedy International,5326,5486", "ORY,Paris-Orly,629,379", "MAD,Adolfo Suarez Madrid-Barajas,1428,1151", "AMS,Amsterdam Schiphol,526,489", "CAI,Cairo International,3779,3584" };

  string searchIATA = "CAI";
  string result = airports.FirstOrDefault(s => s.Contains(searchIATA));

  Console.WriteLine(result);
  Console.ReadKey();