1

I am working on a small code that searches an input text file (of my choice). I am creating a search function. So far I got it to display how many times the search word occurs in the text file and also the line number. I need some help on displaying how the searched word appears in the text file. For example, If I search for the word "the" and it appears as "THE" or "The" in the text file, I would want to display that on my console window.

Any help, advice, or suggestion is appreciated. Thank you in advance!

Here is my code:

string line;
int counter = 0;

Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine();

string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);

int found = 0;

while ((line = myFile.ReadLine()) != null)
{
    counter++;
    if (line.Contains(userText))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);
Bake
  • 17
  • 3

3 Answers3

1

You can use IndexOf instead of Contains since you want to do case-insensitive search:

if (line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase) != -1)
{
    Console.WriteLine("Found on line number: {0}", counter);
    found++;
}
Harsh
  • 1,309
  • 8
  • 14
0

This might do the trick for you

While ((line = myFile.ReadLine()) != null)
{
    line = line.toUpper();
    counter++;
    if (line.Contains(userText.toUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.SubString(line.IndexOf(userText),userText.Length));
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);

The line which is added here is

line.SubString(line.IndexOf(userText),userText.Length)

It means we need to find a SubString inside the line starting from the index of the first occurence of userText and the till the length of userText length

and if you wanted to only compare the string and show the original string you can use this code

While ((line = myFile.ReadLine()) != null)
{
    string linecmp = line.toUpper();
    counter++;
    if (linecmp.Contains(userText.toUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.SubString(line.IndexOf(userText),userText.Length));
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • This works! Now, I need to integrate the case-insensitivity. Thank you. – Bake Nov 30 '15 at 01:54
  • Added `toUpper()` to both the side to make it case insensitive. – Mohit S Nov 30 '15 at 01:57
  • I'm running into a problem. Say I search for the word "the", it is finding it in the word "their" as well. How do I bypass this problem? – Bake Nov 30 '15 at 01:59
  • So, I added toUpper() and it said start index cannot be less than zero. – Bake Nov 30 '15 at 02:03
  • you can have a look on [String compare C# - whole word match](http://stackoverflow.com/questions/3904645/string-compare-c-sharp-whole-word-match) to bypass `the` and `their` problem – Mohit S Nov 30 '15 at 02:06
  • Is there a way to make it so that the user's input is case insensitive? – Bake Nov 30 '15 at 02:14
  • you can have a look on [Case insensitive 'Contains(string)'](http://stackoverflow.com/questions/444798/case-insensitive-containsstring) – Mohit S Nov 30 '15 at 02:16
  • Wouldn't this line of code ignore the case of the user input: if (line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase) != -1) – Bake Nov 30 '15 at 03:02
0

I have slightly modified your search function to resolve the issue you had before. As you mentioned in the comment, you said it is matching word 'THEIR' with word 'THE'.

Add the following code to resolve the mismatches issue.

string userText = Console.ReadLine() + " ";

Modified Full code below.

string line;
int counter = 0;

Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine() + " ";

string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);

int found = 0;

while((line = myFile.ReadLine()) != null)
{
    line = line.ToUpper();
    counter++;
    if (line.Contains(userText.ToUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.Substring(line.IndexOf(userText)+1,userText.Length));
        found++;
    }
 }

 Console.WriteLine("A total of {0} occurences found", found);
 Console.ReadLine();

Hope this helps you.

Suvethan Nantha
  • 2,404
  • 16
  • 28