|
-
May 10th, 2009, 09:19 PM
#1
Thread Starter
Hyperactive Member
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?
-
May 10th, 2009, 09:24 PM
#2
Addicted Member
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
There aren't many problems that ten gallons of gas and a large nose can't cure. Only problem is, most of us don't have ten gallons of gas lying around.
-
May 10th, 2009, 09:27 PM
#3
Addicted Member
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.
There aren't many problems that ten gallons of gas and a large nose can't cure. Only problem is, most of us don't have ten gallons of gas lying around.
-
May 10th, 2009, 09:29 PM
#4
Addicted Member
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. lol
There aren't many problems that ten gallons of gas and a large nose can't cure. Only problem is, most of us don't have ten gallons of gas lying around.
-
May 10th, 2009, 10:44 PM
#5
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
-
May 11th, 2009, 06:53 AM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 11th, 2009, 04:49 PM
#7
Thread Starter
Hyperactive Member
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?
-
May 11th, 2009, 11:15 PM
#8
Addicted Member
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.
There aren't many problems that ten gallons of gas and a large nose can't cure. Only problem is, most of us don't have ten gallons of gas lying around.
-
May 11th, 2009, 11:18 PM
#9
Thread Starter
Hyperactive Member
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.
-
May 11th, 2009, 11:30 PM
#10
Addicted Member
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
There aren't many problems that ten gallons of gas and a large nose can't cure. Only problem is, most of us don't have ten gallons of gas lying around.
-
May 11th, 2009, 11:43 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|