Results 1 to 7 of 7

Thread: Search String

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Post Search String

    Is there API or a Refrence in VB that will allow me to quickly(as in 1 or 2 lines of code) check a string to see if another string is in it?

    Example:
    Im searching through these 4 lines:


    aaaaaaaaaaaaaaaaa1aaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaa111aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1


    My search string is "11"

    It will return the 3rd line only, since it contains my search string.

    Ive seen it before but i cant find it anywhere, it was 1 line of code that would return true or false.. Thats what i need

    Any help would be greatly appreciated, thank you!

  2. #2
    Lively Member
    Join Date
    Nov 2006
    Location
    San Pedro, CA
    Posts
    104

    Re: Search String

    VB Code:
    1. InStr(<start position>, <string>, <comparison>)

    this will return an integer value to show where the string is.

  3. #3

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Search String

    Quote Originally Posted by PMad
    Is there API or a Refrence in VB that will allow me to quickly(as in 1 or 2 lines of code) check a string to see if another string is in it?

    Example:
    Im searching through these 4 lines:


    aaaaaaaaaaaaaaaaa1aaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaa111aaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1


    My search string is "11"

    It will return the 3rd line only, since it contains my search string.

    Ive seen it before but i cant find it anywhere, it was 1 line of code that would return true or false.. Thats what i need

    Any help would be greatly appreciated, thank you!
    The text was probably in a richtextbox

    VB Code:
    1. Dim lngPos As Long
    2.    lngPos = InStr(1, RTB.Text, "11")
    3.    If lngPos > 0 Then
    4.       MsgBox "Line " & (RTB.GetLineFromChar(lngPos) +1) 'note, 1st line is line index 0 so adjust by 1
    5.    Else
    6.       MsgBox "Not found"
    7.    End If

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Oregon
    Posts
    643

    Thumbs up Re: Search String

    Sorry for such a late reply on this, but let me explain.

    Ive got a file that has some headers in it for a user, then its got the rest of the file information in it.

    There's a form that will load, and it will read the header information for every file in a subdirectory in the applications path, and display certain portions of the information on a ListView.

    Above the ListView box there's a series of text boxes, each are for searching through the list of files that came up. You can search by first name, last name, address, phone number, company name, company phone number, company address, the unique ID number, all kinds of things, and it will search through each field.

    Well, this directory will have over 1000 files in it within the next year, so each time i search, it may take some time for each search, but im trying to find the most efficient way to do this so it will be the fastest.

    So for example, if you search the first name field for "ri", then it will return every entry that contains the letters "ri" anywhere in the first name.

    Right now, i go through a couple of For~Next loops. First one to count the rows, 2nd one inside the first that goes the len(firstnamefield) and uses mid$ to scan through each one.. But this method can be fairly slow, not certain if there's a faster way, but it really seems like there should be.

    I hope this clarified everything better, thank you for the help so far everybody

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Search String

    Maintain a database file (an .mdb file would be the easiest - you can create it and maintain it in your code) that has one record per file in the directory, with one field for each item in the header, and one field with the file name. Then a simple SQL Select statement well give you all the files for the search condition, and it'll be fast.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Search String

    With an external DB, you'll have to sync the database...

    You can wrap up your file operations into a class that does what you just described for each file. Iterate through the directory and create a new instance (store classes in a collection) for each file, just pass the path to the class. Class exposes header, search results, etc as properties/methods. File open/close is done internally in the class.

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