Bake
Bake

Reputation: 17

How to display a string

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

Upvotes: 1

Views: 371

Answers (3)

Suvethan Nantha
Suvethan Nantha

Reputation: 2474

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.

Upvotes: 0

Mohit S
Mohit S

Reputation: 14064

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

Upvotes: 0

Harsh
Harsh

Reputation: 1319

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++;
}

Upvotes: 1

Related Questions