Results 1 to 11 of 11

Thread: Search Text file for a word

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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?

  2. #2
    Addicted Member
    Join Date
    Dec 2002
    Posts
    167

    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.

  3. #3
    Addicted Member
    Join Date
    Dec 2002
    Posts
    167

    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.

  4. #4
    Addicted Member
    Join Date
    Dec 2002
    Posts
    167

    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.

  5. #5
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Search Text file for a word

    read the words into an array. then use .indexof

    vb Code:
    1. Dim lines() As String = IO.File.ReadAllLines(filename)
    2. If Array.IndexOf(lines, word) > -1 Then
    3.     'found word
    4. End If

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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?

  8. #8
    Addicted Member
    Join Date
    Dec 2002
    Posts
    167

    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.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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.

  10. #10
    Addicted Member
    Join Date
    Dec 2002
    Posts
    167

    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.

  11. #11
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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
  •  



Click Here to Expand Forum to Full Width