Results 1 to 8 of 8

Thread: [RESOLVED] Compare string and array

  1. #1

    Thread Starter
    Hyperactive Member goofan's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    296

    Resolved [RESOLVED] Compare string and array

    Someone got an tutor on it or some code to show? cuz i got nothing tried alot and nothing worked so ...

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Compare string and array

    You should elaborate your question first, by providing more details and even codes that you had tried. We don't know what you have in your mind.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Hyperactive Member goofan's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    296

    Re: Compare string and array

    Well its the basic question of how to do it that i dont know...

    Well code sample of ideés would be:
    Code:
    *dim splitcontent() as string
    
    
    splitcontent = split(contents)  '~~~ splitting my string with all the text in it to make it go one word each into the array...
    for r = Lbound(splitcontent()) to Ubound(splitcontent())
    
    if strcomp(Trim$(add.text), Trim$(splitcontent(r)), vbtextcompare) = 0 then
    msgbox "Allready exist"
    exit sub
    end if
    next r
    took ure code from the past threads and changed it "thought might work"

    Hope this helps abit cuz all the other ides i forgott...
    Last edited by goofan; Apr 14th, 2010 at 06:57 AM.

  4. #4

    Thread Starter
    Hyperactive Member goofan's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    296

    Re: Compare string and array

    i got this in array:

    Code:
    Hey
    You
    Are
    Here
    Right
    Now
    And
    ...
    I got this in the string:

    Code:
    Hey
    Code:
    vb Code:
    1. If Hey is found in the array then
    2. msgbox "ID exist allready"
    3. exit sub
    4. end if

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Compare string and array

    The code you have provided will compare the entire line (splitcontent(r)) with the textbox content (add.text).

    Is that what you want ? Did you tried(testing) the code ?

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6

    Thread Starter
    Hyperactive Member goofan's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    296

    Re: Compare string and array

    yes i tried and got subsciption out of range...
    Last edited by goofan; Apr 14th, 2010 at 06:58 AM.

  7. #7

    Thread Starter
    Hyperactive Member goofan's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    296

    Re: Compare string and array

    compare the entire line
    it will not, it will compare the words within it... "right"?

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Compare string and array

    You don't need to Split if all your strings are separated by space.

    Code:
    Dim lngPos As Long, strText As String, strFind As String
    
    ' original content
    strText = "Hey You Are Here Right Now And"
    strFind = "and"
    
    ' add spaces around so first and last words are also surrounded by a space
    strText = " " & strText & " "
    
    ' force spaces around the search keyword as well!
    strFind = " " & strFind & " "
    
    ' now find that word - you will not get a mismatch!
    lngPos = InStr(1, strText, strFind, vbTextCompare")
    
    ' if lngPos > 0 then we found something: this is a short way to write it
    If lngPos Then
        MsgBox "Found"
    Else
        MsgBox "Not Found"
    End If
    In this sample space characters are always in the each side of a word, which can then be effectively used as an accurate separator information. Thus you'll end up doing a bit less work than you would do if you used Split to create and array and manually checked each item. InStr with TextCompare isn't the fastest thing in the world, but depending on what you're doing it should be fast enough.

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