0

I have a list of words. I want the program to scan for multiple words from a text file.

This is what i already have:

int counter = 0;
        string line;
        StringBuilder sb = new StringBuilder();

        string[] words = { "var", "bob", "for", "example"};

        try
        {
            using (StreamReader file = new StreamReader("test.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(Convert.ToChar(words)))
                    {
                        sb.AppendLine(line.ToString());
                    }
                }
            }

            listResults.Text += sb.ToString();
        }
        catch (Exception ex)
        {
            listResults.ForeColor = Color.Red;
            listResults.Text = "---ERROR---";
        }

So i want to scan the file for a word, and if it's not there, scan for the next word...

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
  • 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). – CodeCaster Jan 29 '14 at 18:59
  • Depends on how the file is laid out. Are the words separated by newlines, spaces, some other delimiter? – itsme86 Jan 29 '14 at 18:59

3 Answers3

2

String.Contains() only takes one argument: a string. What your call to Contains(Convert.ToChar(words)) does, is probably not what you expect.

As explained in Using C# to check if string contains a string in string array, you might want to do something like this:

using (StreamReader file = new StreamReader("test.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        foreach (string word in words)
        {
            if (line.Contains(word))
            {
                sb.AppendLine(line);
            }
        }
    }
}

Or if you want to follow your exact problem statement ("scan the file for a word, and if it's not there, scan for the next word"), you might want to take a look at Return StreamReader to Beginning:

using (StreamReader file = new StreamReader("test.txt"))
{
    foreach (string word in words)
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(word))
            {
                sb.AppendLine(line);
            }
        }

        if (sb.Length == 0)
        {
            // Rewind file to prepare for next word
            file.Position = 0;
            file.DiscardBufferedData();   
        }
        else
        {
            return sb.ToString();
        }
    }
}

But this will think "bob" is part of "bobcat". If you don't agree, see String compare C# - whole word match, and replace:

line.Contains(word)

with

string wordWithBoundaries = "\\b" + word + "\\b";
Regex.IsMatch(line, wordWithBoundaries);
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0
StringBuilder sb = new StringBuilder();             
string[] words = { "var", "bob", "for", "example" };
string[] file_lines = File.ReadAllLines("filepath");
for (int i = 0; i < file_lines.Length; i++)         
{                                                   
    string[] split_words = file_lines[i].Split(' ');
    foreach (string str in split_words)             
    {                                               
        foreach (string word in words)              
        {                                           
            if (str == word)                        
            {                                       
                sb.AppendLine(file_lines[i]);       
            }                                       
        }                                           
    }                                               
}                                                   
mnshahab
  • 770
  • 7
  • 16
0

This works a treat:

var query =
    from line in System.IO.File.ReadLines("test.txt")
    where words.Any(word => line.Contains(word))
    select line;

To get these out as a single string, just do this:

var results = String.Join(Environment.NewLine, query);

Couldn't be much simpler.


If you want to match only whole words it becomes only a little more complicated. You can do this:

Regex[] regexs =
    words
        .Select(word => new Regex(String.Format(@"\b{0}\b", Regex.Escape(word))))
        .ToArray();

var query =
    from line in System.IO.File.ReadLines(fileName)
    where regexs.Any(regex => regex.IsMatch(line))
    select line;
Enigmativity
  • 113,464
  • 11
  • 89
  • 172