Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all,

 

Working on a program to search a write file.

 

Im not sure on how to go about doing this.

 

Is the StreamReader the way to go and if so how?

 

What im trying to do is set it up so i can type in a textbox a word to search for. If the word is found then msgbox found word.

 

Any ideals on this?

 

 

Thanks

  • *Experts*
Posted

You might consider using a RichTextBox class to load the RTF file in;

then it will be just a matter of searching the Text property.

Dim rtb As New RichTextBox()
rtb.LoadFile("C:\SomeFile.txt")

If rtb.Text.IndexOf("Search For This!") > -1 Then
   MessageBox.Show("Found")
End If

Posted

VolteFace,

 

i have over 1200 files to search for a word.

 

I wanting to serch the file for that word first then im going to output a found this many words in this file. then go from there.

 

but the above works for another program im working on.

 

Any ideals?

 

thanks

  • *Experts*
Posted

Well, you can use a loop and keep searching the RTB text for that

word, each time starting at the previously found instance.

 

So, for example,

Dim position As Integer
Dim count As Integer

position = myText.IndexOf("moo")
Do Until position = -1
 count += 1
 position = myText.IndexOf("moo", position + ("moo").Length)
Loop

It is untested and is probably a bit flawed, but you should

get the idea. Once the loop completes, 'count' will contain the number

of times the string was found.

 

Normally I would recommand that you use the Split method

of a String (using the word to find as delimeter), and checking the Length

of the returned array, but it seems the Split method in .NET only accepts

Char (single characters) as a delimeter, so this way is just

as good I think.

 

 

Loop

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...