11 Replies - 52534 Views - Last Post: 05 November 2011 - 03:18 AM Rate Topic: -----

#1 brockcooper1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Search for a string in a text file

Posted 02 November 2008 - 08:34 PM

hello, this is my first post. And i hope you can help me, i have been looking for about 2 hours now and cant find out how to search a text file for a specific word or string. Also when and if it finds it is it possible to have it highlight it? hope some one can help me :)

Here is what i have, it dose not work :(
richTextBox1.Find(textBox1.Text);

This post has been edited by brockcooper1: 02 November 2008 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Search for a string in a text file

#2 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Search for a string in a text file

Posted 02 November 2008 - 10:24 PM

Try this

        private void searchFile(String file, String searchText)
        {
            //Declare reader as a new StreamReader with file as the file to use
            System.IO.StreamReader reader = new System.IO.StreamReader(file);
            //Declare text as the reader reading to the end
            String text = reader.ReadToEnd();
            //If the searchText is a match
            if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText))
            {
                //Highlight the Text
                richTextBox1.SelectedText = searchText;
            }
            else
            {
                //Show that it was not a match
                MessageBox.Show("Sorry, but " + searchText + " could not be found in the given file", "No Results");
            }
        }



Hope this helps

This post has been edited by gbertoli3: 02 November 2008 - 10:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 brockcooper1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Re: Search for a string in a text file

Posted 03 November 2008 - 02:47 PM

thanks mate, but can u give me a working example, insted of having "file" and "file string" and things like that? can you do it with like textbox1 and richtextbox1 and stuff like that, otherwise i get confused :(
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Search for a string in a text file

Posted 03 November 2008 - 04:10 PM

Would it help if I showed you how to use it?

Use it like so:
//File Locatoin

String FileLocation = "File Location Here";
//or
String FileLocation = fileTextBox.Text;

//Text to Find

String TextToFind = "Text You Want To Find Here";
//or
String TextToFind = searchTextBox.Text;

//Use

searchFile(FileLocation, TextToFind);



Hope this helps

This post has been edited by gbertoli3: 03 November 2008 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 brockcooper1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Re: Search for a string in a text file

Posted 03 November 2008 - 08:54 PM

You are a GOD ;) but 1 thing it doesn't highlight text for some reason, it make a new charter that you have entered for it to search, for example in i search "S" and there is an s it will add an "S" but it will not highlight it :blink:
Sorry for the 20 questions, I'm kinda new to C#
Was This Post Helpful? 0
  • +
  • -

#6 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Search for a string in a text file

Posted 03 November 2008 - 09:05 PM

Are you saying that it will add an S to the end of the Text?
Was This Post Helpful? 0
  • +
  • -

#7 brockcooper1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Re: Search for a string in a text file

Posted 03 November 2008 - 09:56 PM

yes
Was This Post Helpful? 0
  • +
  • -

#8 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Search for a string in a text file

Posted 03 November 2008 - 09:57 PM

Does your code anywhere say:
richTextBox1.Text += "S";
//or
richTextBox1.Text = richTextBox1.Text + "S";



or something like that?

This post has been edited by gbertoli3: 03 November 2008 - 09:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 brockcooper1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 08

Re: Search for a string in a text file

Posted 04 November 2008 - 04:24 AM

no , here is what i have

private void button4_Click(object sender, EventArgs e)
		{
			searchFile("C:\\Site Loggin Info\\Sites.txt", textBox1.Text);
		}
///////////////////////////////////////////////////////////////////////////////
private void searchFile(String file, String searchText)
		{
			//Declare reader as a new StreamReader with file as the file to use
			System.IO.StreamReader reader = new System.IO.StreamReader(file);
			//Declare text as the reader reading to the end
			String text = reader.ReadToEnd();
			//If the searchText is a match
			if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText))
			{
				//Highlight the Text
			   richTextBox1.SelectedText = searchText;
			}
			else
			{
				//Show that it was not a match
				MessageBox.Show("Sorry, but " + searchText + " could not be found in the given file", "No Results");
			}
		}

Was This Post Helpful? 0
  • +
  • -

#10 gbertoli3   User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,166
  • Joined: 23-June 08

Re: Search for a string in a text file

Posted 04 November 2008 - 07:47 AM

Well the only thing I can think of would be deleting the last character.
String text = richTextBox1.Text;
text.Remove(richTextBox1.Text.Length - 1, 1);



Hope this helps
Was This Post Helpful? 0
  • +
  • -

#11 johndmeyer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-November 11

Re: Search for a string in a text file

Posted 03 November 2011 - 02:03 PM

I know this thread has been dead for a long time, but I came across it today. Just make sure you remove the html formating tags from the code: <b></b> in ...system.text.regularexpressions.regex.ismatch...
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6258
  • View blog
  • Posts: 24,026
  • Joined: 23-August 08

Re: Search for a string in a text file

Posted 05 November 2011 - 03:18 AM

That's a bug in the forum software.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1