Using a string builder, i've read a file.. How do i find if the given string is present in the file or not?

16,246

Solution 1

using (System.IO.StreamReader Reader = new System.IO.StreamReader("C://myfile2.txt"))
{
      string fileContent = Reader.ReadToEnd();
       if (fileContent.Contains("your search text"))
           return true;
       else
           return false;
}

Solution 2

How about an one-liner:

 bool textExists = System.IO.File.ReadAllText("C:\\myfile2.txt").Contains("Search text");

That should do the trick :)

Solution 3

In the example you show - there is no point loading into a StringBuilder in the first place. You already return the entire string in the call to StreamReader.ReadToEnd, so you may as well just check if this contains your substring.

StringBuilder is very useful if you are changing and mutating the string - but in this use case, you are not.

How about this:

private bool FileContainsString(string file, string substring)
{
    using (StreamReader reader = new StreamReader("C://myfile2.txt"))
    {
        return reader.ReadToEnd().Contains(substring);
    }
}

If you definitely want the string builder, then something like this:

private bool FileContainsString(string file, string substring)
{
    using (StreamReader reader = new StreamReader("C://myfile2.txt"))
    {
        var sb = new StringBuilder(reader.ReadToEnd());
        return sb.ToString().Contains(substring);
    }
}

but in this specific scenario the StringBuilder is not actually doing anything useful.

Solution 4

You can read the whole file into a string with File.ReadAllText. Note that this does load the whole file at once, and thus costs a lot of memory if the file is large:

string content = File.ReadAllText(@"C:\myfile2.txt");
Console.WriteLine(content.Contains("needle"));

If your needle doesn't span multiple lines you can go with:

IEnumerable<string> content = File.ReadLines(@"C:\myfile2.txt");
Console.WriteLine(content.Any(line => line.Contains("needle")));

This only needs to keep one line in memory at a time, and thus scaled to larger files.

Share:
16,246
3692
Author by

3692

Updated on June 05, 2022

Comments

  • 3692
    3692 almost 2 years

    I've read a file into StringBuilder. When I give a string as an input, if the word is present in the file, the output should be true.. If its not present, it should be false.. How do i do it? Could someone help me with the code? I've written the program till here.. How do i go further? Thanks alot.. :)

    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader Reader = new StreamReader("C://myfile2.txt"))
            {
                StringBuilder Sb = new StringBuilder();
                Sb.Append(Reader.ReadToEnd());
                {
                    Console.WriteLine("The File is read");   
                }
            }
        }
    }
    
  • moribvndvs
    moribvndvs about 12 years
    In this example, you don't use Sb.
  • Rob Levine
    Rob Levine about 12 years
    or even just return fileContent.Contains("your search text")
  • Rob Levine
    Rob Levine about 12 years
    Wrong language - this is C#. In any case - you don't need the if..else. Just return the result of contains
  • Virus
    Virus about 12 years
    sorry, I have just put it in vb. In case you want to do anything else on return of true of false, we need if..else.
  • CodesInChaos
    CodesInChaos about 12 years
    return true; else return false; one of my favourite patterns.
  • 3692
    3692 about 12 years
    @juergen Thank u so much.. :)
  • juergen d
    juergen d about 12 years
    @CodeInChaos: I used else to make the workflow clearer for a beginner.
  • 3692
    3692 about 12 years
    Actually yes, its true i dont need a string builder.. Thank u so much.. :)