Search Text file for a word
I have a txt file that lists about 170,000 words -- one word on each line. What's the easiest and fastest way for me to check if a work typed into a textbox exists in that word list file?
I was thinking of starting with the instr function. If it returns 0, then the word obviously doesn't exist in the file. But if instr returns something greater than 0, it doesn't mean the word does exist in the file. For example, "fr" is not in the word list but would return a positive result because "frown" is a word in the list.
Does that mean that after getting a positive result from instr, that I have to check every single word? I could load the words into an array with no problem but is there another way?
Re: Search Text file for a word
Converting it to a database, I would think. Although I'm no expert on data manipulation, as my posts have proved. My data programming is severely lacking. Always was more of a graphics/game programmer, just got into data and controls about a week ago. :s
Re: Search Text file for a word
I dont know if a string list can hold that many items, maybe some of the experts will help you out with that. If not, try reading up on Lists. Might be someway to read them all into a list or multiple lists split up in all words of the same first letter, depending on the max allowable strings in a list. Just throwing out some suggestions here, as I stated, I'm no expert. :)
Re: Search Text file for a word
By the way, nice to see someone from the old school. I'm referring to your join date. Dec. 02 here. :thumb: lol
Re: Search Text file for a word
I'm not familiar with InStr but if you use .IndexOf than you can use this:
Code:
DocumentText.IndexOf(Environment.NewLine & "WORD" & Environment.NewLine)
If it returns anything greater than -1 than the word is in the document. The only problem with this code is that if the word is the very last one in the document it won't work. If you can put an extra line at the bottom of the document that will fix it though, so for instance:
Code:
DocumentText &= Environment.NewLine
And then search it
Re: Search Text file for a word
read the words into an array. then use .indexof
vb Code:
Dim lines() As String = IO.File.ReadAllLines(filename)
If Array.IndexOf(lines, word) > -1 Then
'found word
End If
Re: Search Text file for a word
Good one .paul.
What if I wanted to look for words with wildcards? For example, I typ "?itch" into my text box and I want the program to return all the 5-letter words that end with "itch". (Yeah I know!)
I suppose I would use the same logic as above but replace the ? with each letter from "a" to "z". I suppose further that when there are more than one "?" characters entered that I could use recursion to check all the possibilities. Any suggestions? on that one?
Re: Search Text file for a word
I would probably still suggest taking the time to put them in a db, as that seems the most logical thing to do for 170,000 strings. And it would make the list highly portable as well (i.e. easily accessible from other programs.) But that's just me, I'm a db freak at the moment, cause I'm just learning it...lol.
Re: Search Text file for a word
As I've done the program now, it will work with any text file list of words. I've never been big on database. Maybe some day though.
Re: Search Text file for a word
Fair enough, I wouldn't suggest getting into them unless you like a challenge and have a LOT of time researching syntax and such. I'm having a heck of a time, but I can't stop. The more I go, the more I learn. I even find myself now actually helping with db problems, which a few days ago I'd have never thought possible...lol
Re: Search Text file for a word
Convert it to an XML file and then use VTD-Xml. I use it for almost everything involving XML nowadays because it's blindingly fast.