Results 1 to 1 of 1

Thread: VB - Some functions that makes string parsing easier

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    VB - Some functions that makes string parsing easier

    The next functions makes string parsing a “piece of cake”.

    The first word in the function names, is how the function does the search (ie Left = Instr and Right = InStrRev), and the right word of the function is what part of the string the function returns...

    These functions makes string parsing easier because it returns the result as a string instead of the string position.

    And one last thing to mention: the functions do NOT include the string to search in the result.

    Copy and paste the code into a module ( .bas ) file
    Code:
    Option Explicit
    
    Public Enum IfStringNotFound
        ReturnOriginalStr = 0
        ReturnEmptyStr = 1
    End Enum
    
    ' Search from end to beginning, and return the left side of the string
    Public Function RightLeft(ByRef Str As String, RFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long
        
        K = InStrRev(Str, RFind, , Compare)
        
        If K = 0 Then
            RightLeft = IIf(RetError = ReturnOriginalStr, Str, "")
        Else
            RightLeft = Left(Str, K - 1)
        End If
    End Function
    
    ' Search from end to beginning and return the right side of the string
    Public Function RightRight(ByRef Str As String, RFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long
        
        K = InStrRev(Str, RFind, , Compare)
        
        If K = 0 Then
            RightRight = IIf(RetError = ReturnOriginalStr, Str, "")
        Else
            RightRight = Mid(Str, K + 1, Len(Str))
        End If
    End Function
    
    ' Search from the beginning to end and return the left side of the string
    Public Function LeftLeft(ByRef Str As String, LFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long
        
        K = InStr(1, Str, LFind, Compare)
        If K = 0 Then
            LeftLeft = IIf(RetError = ReturnOriginalStr, Str, "")
        Else
            LeftLeft = Left(Str, K - 1)
        End If
    End Function
    
    ' Search from the beginning to end and return the right side of the string
    Public Function LeftRight(ByRef Str As String, LFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long
        
        K = InStr(1, Str, LFind, Compare)
        If K = 0 Then
            LeftRight = IIf(RetError = ReturnOriginalStr, Str, "")
        Else
            LeftRight = Right(Str, (Len(Str) - Len(LFind)) - K + 1)
        End If
    End Function
    
    ' Search from the beginning to end and return from StrFrom string to StrTo string
    ' both strings (StrFrom and StrTo) must be found in order to be successfull
    Public Function LeftRange(ByRef Str As String, StrFrom As String, StrTo As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long, Q As Long
        
        K = InStr(1, Str, StrFrom, Compare)
        If K > 0 Then
            Q = InStr(K + Len(StrFrom), Str, StrTo, Compare)
            
            If Q > K Then
                LeftRange = Mid(Str, K + Len(StrFrom), (Q - K) - Len(StrFrom))
            Else
                LeftRange = IIf(RetError = ReturnOriginalStr, Str, "")
            End If
        Else
            LeftRange = IIf(RetError = ReturnOriginalStr, Str, "")
        End If
    End Function
    
    ' Search from the end to beginning and return from StrFrom string to StrTo string
    ' both strings (StrFrom and StrTo) must be found in order to be successfull
    Public Function RightRange(ByRef Str As String, StrFrom As String, StrTo As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
        Dim K As Long, Q As Long
        
        K = InStrRev(Str, StrTo, , Compare)
        If K > 0 Then
            Q = InStrRev(Str, StrFrom, K, Compare)
            
            If Q > 0 Then
                RightRange = Mid(Str, Q + Len(StrFrom), (K - Q) - Len(StrTo))
            Else
                RightRange = IIf(RetError = ReturnOriginalStr, Str, "")
            End If
        Else
            RightRange = IIf(RetError = ReturnOriginalStr, Str, "")
        End If
    End Function
    Last edited by CVMichael; Jan 26th, 2009 at 03:43 PM. Reason: Some spelling mistakes

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