Results 1 to 4 of 4

Thread: Find InStr: (cant explain)

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Find InStr: (cant explain)

    I wrote this code, that will search in a string in between an opening and closing phrase:
    VB Code:
    1. Private Sub Form_Load()
    2.     MsgBox FindInStr("||", "::", "Cat alpha Pensi france catarax || alabastar cheese gato faggle :: google cheese")
    3. End Sub
    4.  
    5. Public Function FindInStr(StrOpening As String, StrClosing As String, lpString As String) As String
    6.         Dim x As Integer, y As Integer, InStrStr As String, FinalStr As String
    7.             x = InStr(lpString, StrOpening)
    8.                 x = x + Len(StrOpening)
    9.                FinalStr = Mid(lpString, x, Len(lpString) - x)
    10.                 y = InStr(FinalStr, StrClosing)
    11.                     InStrStr = FinalStr
    12.                    FinalStr = Mid(FinalStr, y, Len(FinalStr) - y + 1)
    13.                 FindInStr = Replace(InStrStr, FinalStr, vbNullString): FindInStr = Trim(FindInStr)
    14. End Function
    This will return "alabastar cheese gato faggle"

    This also didnt work as i wanted, i didnt want to use replace, but i guess i had to

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Find InStr: (cant explain)

    This seems to do the same things without the REPLACE...

    Why did you want the REPLACE?

    Code:
    Private Sub Form_Load()
        MsgBox FindInStr("||", "::", "Cat alpha Pensi france catarax || alabastar cheese gato faggle :: google cheese")
    End Sub
    
    Public Function FindInStr(StrOpening As String, StrClosing As String, lpString As String) As String
            Dim x As Integer, y As Integer, InStrStr As String, FinalStr As String
                x = InStr(lpString, StrOpening)
                x = x + Len(StrOpening)
                y = InStr(x, lpString, StrClosing)
                FindInStr = Trim(Mid(lpString, x, (y - x) - 1))
    End Function

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find InStr: (cant explain)

    i didnt want to use replace
    i couldnt figure out how this last bit:
    FindInStr = Trim(Mid(lpString, x, (y - x) - 1))

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Find InStr: (cant explain)

    Check out the LeftRange function I posted in this thread:
    VB - Some functions that makes string parsing easier
    I use thouse functions in 99% of my applications, very usefull functions...

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