Results 1 to 14 of 14

Thread: [RESOLVED] Instr function

  1. #1

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Resolved [RESOLVED] Instr function

    Hi All,
    I need to evaluate if a string that contains numbers has a certain number. For this purpose I thought that I can use the Instr function but it seems the approach is wrong. My example looks like below :
    Code:
    Dim S As String, p As Long
    S = "1825, 8253"
    p = InStr(1, s, "825")
    Either I set the comparison argument as binary or as text each time the function returns "2" while I expected to have "0". That happens because Instr does read each substring sequence inside S string...
    Is it possible to compare string numbers as if I compare numbers only? I would like to avoid any loop for this task. Thank you.
    Last edited by Daniel Duta; Sep 1st, 2014 at 09:17 AM.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Instr function

    The result is correct. "825" is found at position 2 in the string "1825, 8253"

    What kind of "magic" do you need?

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Instr function

    Some code from a library to work with "multivalue" strings.
    In your case the Separator is ", "

    Code:
    Public Function mvLocate(sString As String, sSearch As String, sSeparator As String) As Long
      Dim lFieldNo As Long
      Dim lPos1 As Long, lPos2 As Long, lStart As Long
      
      If Len(sString) = 0 Or Len(sSearch) = 0 Then
        Exit Function
      End If
      
      lPos2 = 1
      lPos1 = 1
      lStart = 1
      Do While lPos1 > 0
        lFieldNo = lFieldNo + 1
        lPos1 = InStr(lPos2, sString, sSeparator, vbBinaryCompare)
        lStart = lPos2
        If lPos1 = 0 Then
          If Mid$(sString, lStart) = sSearch Then
            mvLocate = lFieldNo
            Exit Do
          End If
        Else
          If Mid$(sString, lStart, lPos1 - lStart) = sSearch Then
            mvLocate = lFieldNo
            Exit Do
          End If
        End If
        lPos2 = lPos1 + 1
      Loop
      
    End Function

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Instr function

    Function you seem to need would to be a mini-wordprocessor of sorts
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Instr function

    Quote Originally Posted by LaVolpe View Post
    Function you seem to need would to be a mini-wordprocessor of sorts
    A Regular Expression engine might qualify as such.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: Instr function

    Hi Arno,
    Thank you for reply. Your function is not very different than other based on Split function, which is even smaller. After that you can loop the array and if n1=n2 than exit for/sub. The idea is it involves a loop and I wanted to avoid this.
    The result is correct. "825" is found at position 2 in the string "1825, 8253"
    Unfortunately, you are right. I wanted to get "0" for that comparison because "825" <> "1825" but I think it is not possible to do this using Instr function as long as it returns the position of a substring in a string.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Instr function

    If you only need to know whether it is present or not:

    Code:
    Option Explicit
    
    Function Present(ByVal Numbers As String, ByVal Target As String) As Boolean
        Present = " " & Numbers & " " Like "*[ ,]" & Target & "[ ,]*"
    End Function
    
    Private Sub Form_Load()
        Dim Numbers As String
    
        Numbers = "1825, 8253"
        MsgBox Present(Numbers, "825")
        MsgBox Present(Numbers, "8253")
    
        Unload Me
    End Sub

  8. #8

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: Instr function

    Hello Dille,
    I was about to start a function based on the native Split function when I read your post. It is exactly what I needed. Only to check the presence of a string number in other. So it is not a mini-wordprocessor but a simple, smart and beautiful solution. I think you had the best maths grades at the school.
    Thank you for this solution. It is a good example of ingenuity.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Instr function

    Glad it helps.

    I saw "regular expressions" mentioned above and started gagging, so I had to post something to get the bad taste out of my mouth.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Instr function

    LaVolpe mentioned a mini word processor. Regular Expressions immediately came to my mind. It is severe overkill for something this simple but it is immensely powerful for things that aren't that simple. It really is like a mini word processor.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Instr function

    Or like a mini virtual coprocessor programmed in Martian assembly language. Some swear by it, most swear at it.

  12. #12

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    396

    Re: [RESOLVED] Instr function

    PS. Honestly, it is one of the most interesting solution I have ever seen. I am sure not many students in informatics or even experienced programmer could solve this problem in a such a simple manner. I have shared this solution to my office colleagues and everybody recognized it is very ingenious. No more words to say. Just hat's off to you !

  13. #13
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Instr function

    Your function is not very different than other based on Split function, which is even smaller. After that you can loop the array and if n1=n2 than exit for/sub. The idea is it involves a loop and I wanted to avoid this.
    Less code is not per definition more efficient.
    Using the Split() would use more memory and processing time.
    My code maybe uses more statements but is quite efficient when dealing with large datasets.

  14. #14
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: [RESOLVED] Instr function

    I have a "tester" in codebank that take a line of VB code and embed code to colorize it, without storing each part but the last part before bound the colorize code. So It works like a filter. If you want only the numbers you can keep numbers for the final output. Also this routine found hex values and floating point numbers, strings, labels (knowing which label is a vb word) and symbols

    http://www.vbforums.com/showthread.p...es-for-changes

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