CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2002
    Posts
    79

    Search String in a text file

    Hi,

    Anyone have any idea on how to search a particular string in a text file?
    If using StreamReader, can i do that?
    I am writting a function to return true,false; if i could find the particular string.
    Please advice. Thanks.

    e.g, i wan to open sample.txt to look for "nine"? do i need to read line by line?or i have other option? thanks

    sample.txt
    one,1
    two,2
    three,3
    ..............

  2. #2
    Join Date
    Dec 2005
    Posts
    282

    Re: Search String in a text file

    ReadToEnd or ReadLine and then string.contains

    Regards
    Hansjörg

  3. #3
    Join Date
    Nov 2004
    Posts
    105

    Re: Search String in a text file

    class TestFileIO
    {
    static void Main()
    {
    string fileName = "test.txt"; // a sample file name

    // Delete the file if it exists.
    if (System.IO.File.Exists(fileName))
    {

    string s = "";
    System.IO.StreamReader sr = System.IO.File.OpenText(fileName)
    while ((s = sr.ReadLine()) != null)
    {
    if(s.contains("Sample text")
    MessageBox.show("Message");

    }
    }
    }
    }

    just like u can get this in MSDN

    http://msdn2.microsoft.com/en-us/library/ms228592.aspx

    May be it will help you

    regards
    Ravi

  4. #4
    Join Date
    Mar 2002
    Posts
    79

    Re: Search String in a text file

    thank you so much, i would try it out......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured