9 Replies - 1879 Views - Last Post: 10 December 2011 - 03:25 AM

#1 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Search Text file for String

Posted 19 September 2007 - 03:04 PM

Description: input the path to the text file and the string to search for.Searches a text file for a string by using Regex.IsMatch
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace searchTxtFile
{
    class searchTxt
    {
        static void Main()
        {
            string fName = @" ";//path to text file
            StreamReader testTxt = new StreamReader(fName);
            string allRead = testTxt.ReadToEnd();//Reads the whole text file to the end
            testTxt.Close(); //Closes the text file after it is fully read.
            string regMatch = " ";//string to search for inside of text file. It is case sensitive.
            if (Regex.IsMatch(allRead,regMatch))//If the match is found in allRead
            {
                Console.WriteLine("foundn");

            }
            else
            {
                Console.WriteLine("not foundn");
            }

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Search Text file for String

#2 demo123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 30-January 08

Re: Search Text file for String

Posted 30 January 2008 - 01:06 AM

Buggy snippet, without object dispose and etc., not optimal written
Was This Post Helpful? 0
  • +
  • -

#3 ZachR  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 126
  • Joined: 15-June 08

Re: Search Text file for String

Posted 17 June 2008 - 10:48 PM

Thanks, This helped a lot.
Was This Post Helpful? 0
  • +
  • -

#4 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: Search Text file for String

Posted 26 August 2008 - 08:28 PM

Thanks this was a big help!
Was This Post Helpful? 0
  • +
  • -

#5 bsit-ia barkada  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 28-September 08

Re: Search Text file for String

Posted 03 October 2008 - 04:28 AM

we want a code that is for file system like library system and service record system.
Was This Post Helpful? 0
  • +
  • -

#6 lordelf2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-October 08

Re: Search Text file for String

Posted 24 November 2008 - 11:19 AM

Can I count the occurrence of a string in text file?
Was This Post Helpful? 0
  • +
  • -

#7 carck3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-March 09

Re: Search Text file for String

Posted 29 March 2009 - 04:46 AM

Thanks! That was so helpful.
Was This Post Helpful? 0
  • +
  • -

#8 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-September 07

Re: Search Text file for String

Posted 26 June 2009 - 03:10 AM

How to search if I have string like this Request.QueryString("id")
Was This Post Helpful? 0
  • +
  • -

#9 [email protected]  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 04-December 09

Re: Search Text file for String

Posted 04 December 2009 - 09:57 PM

I am uploading word,pdf files and storing these file in a drive© within a folder and store path of this folder into database. I want to search any word(containts,texts) within document(word,pdf files) and it should display all the files(word,pdf files) containing that word. any help Thanks Prakash Gupta [email protected]
Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 183
  • View blog
  • Posts: 785
  • Joined: 27-October 11

Re: Search Text file for String

Posted 10 December 2011 - 03:25 AM

You could have made use of the using statement with the StreamReader, also you could have hardcoded the path (not using the variable fName) that way you could avoid the textTx.Close() and you would make it easier on the garbage collector
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1