0

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() ;
        }


    }
}

}

  • 2
    This line itself is wrong. lines.Any(stringToCheck.Contains) You're iterating over all the line though you're in a loop also stringToCheck is more than one value - Contains may fail here as well – Gururaj May 09 '17 at 14:15
  • Look first at the method `stringA.Contains(stringB)`. What does it do? Then -- lets forget for a moment about string.Contains -- what does Linq's `list.Any(someCriteria)` do? –  May 09 '17 at 14:16
  • @Gururaj you should post that as an answer not a comment. – Scott Chamberlain May 09 '17 at 14:16
  • @ScottChamberlain - I don't want to get into writing code unless they try by themselves and fail – Gururaj May 09 '17 at 14:18
  • Possible duplicate of [Using C# to check if string contains a string in string array](http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array) – EpicKip May 09 '17 at 14:26
  • @Gururaj I see now why that line is wrong, i should be checking string s against stringtocheck instead. Something like s.Any(stringToCheck.Contains but I know that won't work as .Any requires it to be a string array and not a string. – user3394486 May 09 '17 at 14:29
  • Your if statement will change if (stringToCheck.Contains(s)) and proceed with printing found or not – Gururaj May 09 '17 at 14:37
  • @Gururaj Thanks for the help, I was able to understand what I was doing wrong. – user3394486 May 09 '17 at 14:38
  • Instead of updating your question with what was wrong and how you changed it, you should mark whichever answer helped you. – Rufus L May 09 '17 at 14:43
  • @RufusL its was Gururaj's comment that helped me the most. Is there a way I can mark that as the answer? – user3394486 May 09 '17 at 14:48
  • No, but Felipe's answer has the exact code that helped you, and came in just one minute after Gururaj's first comment – Rufus L May 09 '17 at 14:55
  • @RufusL Cheers, updated it now – user3394486 May 09 '17 at 15:01

4 Answers4

3

You could use Any over the list of states to check if there is any string for each state which contains on the line. For sample:

    if (stringToCheck.Any(x = > s.Contains(x))
    {
      // ...
    }
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

you could do something like this:

if(stringToCheck.Any(e => s.Contains(e)){
   //
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

You could do something like this:

string[] stringToCheck = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado" };

//Some test lines
string[] lines = { "sadnaskjd Alabama", "sadasd Arizona", "asdasdaer" };

//A bool telling me if I found anything, 
//you could skip the bool and just use else in the foreach :)
bool contains = false;


foreach ( var check in stringToCheck )
{
    if ( lines.Any( l => l.Contains( check ) ) )
    {
        Console.WriteLine( "Found substring" );
        Console.WriteLine( s );
        contains = true;
    }  
}

if ( contains == false )
{
    Console.WriteLine("Could not find substring");
    Console.ReadLine();
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
0

Try this idea:

if (line.Any(x=>CheckKeyWordsInLine(line, stringToCheck)){
     //The line contains one of the keywords
}else{
     //The line does not contain any of the keywords
}

Then define the CheckKeyWordsInLine function as:

bool CheckKeyWordsInLine(string line, string[] keywords){

 foreach(var key in keywords)
 {
   if(line.Contains(key))
   {
      return true;
   }
 }
 return false;
}