Results 1 to 12 of 12

Thread: [RESOLVED] find exact string using instr, ignoring partials

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    176

    Resolved [RESOLVED] find exact string using instr, ignoring partials

    Hello,
    I am trying to select words that end in "ion" while ignoring words that contain "ion" but don't end in "ion". This code works to detect "ion" and ignore "iones", but it is looking at the last word in the textbox to find the string. So if I type in "iones ion" it will detect the last "ion", but if I type in "ion iones" it ignores the "ion" before the "iones", because it is focusing on the "iones" at the end of the textbox. How can I get the program to do this?

    Code:
    Private Sub Command2_Click()
    
    If InStr(1, Text1.Text, "ion", vbTextCompare) > 0 Then
    If Right(Text1.Text, 3) = "ion" Then
    
    List2.AddItem "ion is found"
    
    End If
    End If
    
    End Sub
    Thanks!

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: find exact string using instr, ignoring partials

    You could split your string into word and then for each word check if it ends with "ion".
    Make sure to trim the spaces and unwanted characters like the period "." or the comma ",".

    Another way would be to use regular expressions.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: find exact string using instr, ignoring partials

    Yet another way is to verify that the letter following "ion" (if it exists) is not a-z or A-Z. To select all "ion"'s in a given string you'd need to call InStr repeatedly with ever-increasing starting indexes and filter out values according to the above condition.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: find exact string using instr, ignoring partials

    Yet another way would be to use the Like Operator instead of InStr
    Code:
    Option Explicit
    
    Private Sub Command_Click()
    Dim str As String
    Dim strA() As String
    Dim intI As Integer
    str = "xion ions relation relations ion ions"
    strA = Split(str, " ")
    For intI = 0 To UBound(strA)
        If strA(intI) Like "*ion" Then Debug.Print strA(intI)
    Next intI
    End Sub
    Last edited by Doogle; Jan 29th, 2011 at 03:02 AM.

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: find exact string using instr, ignoring partials

    Quote Originally Posted by Doogle View Post
    Yet another way would be to use the Like Operator instead of InStr
    I like that option (pun intended ), though as stlaural mentioned you have to remember to account for periods and commas. With the Like syntax, that's not too hard--just make sure the character after "ion" is non-alphabetic or non-existent.

    I was imagining the OP was doing something like highlighting the occurrences of word suffixes and that they'd want the index of the start of each occurrence in the original text, which is why I suggested iterating InStr's instead of Split (Split makes it hard to recover the index in the case where you've modified parts of the Split array strings). Now that I've thought about it, though, if you let the Like operator handle filtering, recovering the index isn't hard at all.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: find exact string using instr, ignoring partials

    Holijuit! You pundits are trying to make a seemingly simple issue seem so complex. Here, let me try my simplest solution so far. StarsCrea implied that "ion" should be at the end of the word. This implies four possibilities:
    1- There is a space after "ion" and "ion" exists in the middle/start of the string (e.g. Water molecules are formed with an Oxygen ION and two Hydrogen ions.)
    2- A sentence in the string ends at "ion. " while the string carries on with other data too (e.g. Water molecues contain one Oxygen ION. Sand molecules have two Oxygen ions)
    3- The string ends at "ion" (e.g. Water molecules contain one Oxygen ION)
    4- The string ends at "ion." (e.g. Water molecules contain one Oxygen ION.)

    Here is what is my approach about the matter:

    vb Code:
    1. Private Sub Command2_Click()
    2.    If InStr(Text1.Text,"ion ")<>0 Then
    3.       List2.AddItem "ion found at the end of a word"
    4.    ElseIf InStr(Text1.Text "ion. ")<>0 Then
    5.       List2.AddItem "ion found at the end of a sentence"
    6.    ElseIf Right(Text1.Text,3)="ion" Then
    7.       List2.AddItem "ion found at the end of string (Bad grammar. Should contain a period at the end)"
    8.    ElseIf Right(Text1.Text,4)="ion." Then
    9.       List2.AddItem "ion. found at the end of string (Standard English grammar)"
    10.    End If
    11. End Sub

    Note that I've posted the code with the possibilities that arose in my own mind. If there are more possibilities of this, you might want to add more ElseIf lines.
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    176

    Re: find exact string using instr, ignoring partials

    Thank you to all who suggested solutions. Now, If I type in "solutions", it ignores it, since it ends in S, but if I type in "solution", it detects it, even if the "solution" comes before "solutions". I ended up going with Lone_Rebel's solution...

    Thanks again!

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] find exact string using instr, ignoring partials

    My only critism of lone_Rebel's solution is that the If statement can get quite large if you take into account all the possible punctuation marks etc that could legitimately follow the word. eg. ; : ? ! } ] ) -# " ' that's another 11 ElseIf's to code. Also, assuming you're using a multi-line TextBox, you'll have to take the vbCrLf, or vbNewLine, at the end of each line into account.
    Last edited by Doogle; Jan 29th, 2011 at 05:12 AM.

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] find exact string using instr, ignoring partials

    I agree with Doogle. That solution isn't very robust and I'd hate to see it in commercial code because of how many edge cases it fails. But, maybe my standards are too high in this case. Ah well, glad it worked out.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  10. #10
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: [RESOLVED] find exact string using instr, ignoring partials

    I agree with you guys too. This code isn't suitable for commercial apps where you don't have the option to edit the code once you distribute the application on a large scale but should do for school projects and personal use. I think my most accurate and precise attempt would be creating an array of characters which, if appear after "ion" should still make it count (e.g. . , ? ! and other quotation marks). But that would be a bit complex to go with Crea's understanding of VB thats why I went with the relatively simple code which he can understand and modify to his needs.

    For you experts I think I would have used something like this (note that this is not the exact code. It is just a general structure of the coding I'd use if I needed it to be precise.

    Code:
    Const allowedlist As Integer=7
    Dim puncts(1 to allowedlist) As String
    puncts(1)="."
    puncts(2)=","
    puncts(3)=" "
    puncts(4)="?"
    '... and so on till you complete your list of punctuation marks
    
    Function DoesStrExist(ByRef data As String, ByRef srch_str As String) As Integer
       Dim i As Integer
       For i=1 to allowedlist
          If InStr(data,srch_str)<>0 Then
             DoesStrExist=Instr(data,srch_str)
             Exit Function
          End If
       Next i
       If Right(data,len(srch_str)=srch_str) Then
          DoesStrExist=len(data)-len(srch_str)+1
          Exit Function
       End If
       DoesStrExist=0
    End Function
    Last edited by lone_REBEL; Jan 29th, 2011 at 02:00 PM. Reason: Wanted to include a function example to show the experts my approach about the matter when I wanted to do it precisely!
    If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.

    If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.

    For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.

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

    Re: [RESOLVED] find exact string using instr, ignoring partials

    This works with any alphabet that makes a difference with lower & uppercase letters:
    Code:
    Option Explicit
    
    Public Function FindEOS(String1 As String, String2 As String, Optional ByVal Start As Long = 1, Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As Long
        Dim C As String, E As Long, L1 As Long, L2 As Long, P As Long
        L1 = Len(String1)
        L2 = Len(String2)
        P = Start - 1
        Do
            P = InStr(P + 1, String1, String2, Compare)
            If P Then
                E = P + L2
                If E > L1 Then
                    FindEOS = P
                    Exit Do
                Else
                    C = Mid$(String1, E, 1)
                    If AscW(UCase$(C)) = AscW(LCase$(C)) Then
                        FindEOS = P
                        Exit Do
                    End If
                End If
            End If
        Loop While P
    End Function
    
    Private Sub Form_Load()
        Debug.Print FindEOS("medios", "ios") ' 4
        Debug.Print FindEOS("medios(", "ios") ' 4
        Debug.Print FindEOS("mediosä", "ios") ' 0
        Debug.Print FindEOS("medios.", "ios") ' 4
        Unload Me
    End Sub
    It is somewhat fast too and you can easily search for multiple occurances.
    Last edited by Merri; Jan 29th, 2011 at 02:06 PM.

  12. #12
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: [RESOLVED] find exact string using instr, ignoring partials

    wrong thread
    Last edited by Max187Boucher; Feb 10th, 2013 at 01:30 AM.

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