TexG Posted April 13, 2003 Posted April 13, 2003 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 Quote
*Experts* Volte Posted April 13, 2003 *Experts* Posted April 13, 2003 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 Quote
TexG Posted April 13, 2003 Author Posted April 13, 2003 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 Quote
*Experts* Volte Posted April 13, 2003 *Experts* Posted April 13, 2003 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) LoopIt 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.