I am trying to find a substring of states in a file. I open the file and load each line a string at a time. I would then like to check if each string contains one of the states in my substring. It is not working as intended as it keeps returning "Could not find substring" even though I know that the states are in the string. What am I doing wrong?
EDIT: I realise now what the error, this line was completely wrong:
if (lines.Any(stringToCheck.Contains))
It should be like this:
if (stringToCheck.Any(s.Contains))
Thanks for the help guys.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main (string[] args)
{
string[] stringToCheck = {"Alabama","Alaska","Arizona","Arkansas","California","Colorado"};
string[] lines = File.ReadAllLines(@"C:\C# Project\sampledata.dat");
foreach (string s in lines)
{
if (lines.Any(stringToCheck.Contains))
{
Console.WriteLine("Found substring");
Console.WriteLine(s);
Console.ReadLine();
}
else
Console.WriteLine("Could not find substring");
Console.WriteLine(s);
Console.ReadLine() ;
}
}
}
}