Extract substring


Author: Dave
Date: 08.05.19 - 11:36pm



One of the most common tasks you will encounter is the need to extract a string fragment from a larger block.

Its a very simple repetitive task and I generally have just done it inline for many many years. The catch is it does require several tests along the way to make sure it is done safely without error on unexpected input.

Years ago I wrote a StringEx class that handled many things including an extract function, yet I rarely use it since it seems to bulky unless I am really doing some heavy duty string parsing.

I finally got tired and just pulled out the extract routine into its own standalone function for general use. Makes extractions nice safe one liners.

Couple notes I left most args variants for flexibility. marker1 or marker2 can be empty and it will use defaults. I decided to return the extracted string length instead of a simple boolean because its a little bit richer feedback that could be useful (if you were expecting a string of min length in return), just remember to test =0 or > 0 instead of the easier if extract() then or if not. Easy to change to your liking.

The output of the below function is s1 = 's1'; m2 = m2

Private Sub Form_Load()
    
    Dim m2, s1, pos
    Const base = "test <m1> 's1' <m2>"
    
    If Extract(base, "'", "'", s1, True, , pos) > 0 Then
        Debug.Print "s1 = " & s1
    End If
    
    If Extract(base, "<", ">", m2, , pos) > 0 Then
        Debug.Print "m2 = " & m2
    End If
    
    
End Sub

Function Extract(value, marker1, marker2, ByRef outVar, _
                Optional includeMarkers As Boolean = False, _
                Optional Start = 1, _
                Optional ByRef lastPos, _
                Optional method As VbCompareMethod = vbBinaryCompare _
) As Long

    Dim a As Long, b As Long
    
    lastPos = 0
    outVar = Empty
    
    If Len(marker1) = 0 Then
        a = 1
    Else
        a = InStr(Start, value, marker1, method)
        If a < 1 Then Exit Function
    End If
    
    a = a + Len(marker1)
    If Len(marker2) = 0 Then
        outVar = Mid(value, a)
    Else
        b = InStr(a, value, marker2, method)
        If b < 1 Then Exit Function
        
        lastPos = b + Len(marker2)
        outVar = Mid(value, a, b - a)
    End If
    
    If includeMarkers Then outVar = marker1 & outVar & marker2
    Extract = Len(outVar)
    
End Function





Comments: (2)

On 08.08.22 - 4:14pm Dawg wrote:
I didnt really understand the documentation. What is this function supposed to do? Does it? s1 s1 m2 m2 Youve got to be joking.

On 08.08.22 - 4:48pm Dave wrote:
The first example will extract the substring ‘s1’ including the single quote markers. It will set the outvar function argument with this value and the pos argument with its start position in the string. Finally it will return the length of the extracted string as the function return value.

In the second example it will start the string search at the pos passed in (starting after the first strings found offset) and extract the next substring found between the markers

So literally variable s1 will “‘s1’” And variable m2 “m2”

So it sets 2 arguments with values, returns a third value and also saves you from potential exceptions if marker 1 or 2 don’t exist in the string. Generally you would have to perform all of these checks manually every time you want to extract a substring to do it safely.

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 3 + 8 = ? followed by the letter: E 



About Me
More Blogs
Main Site
Posts: (All)
2024 ( 3 )
2023 ( 9 )
2022 ( 4 )
2021 ( 2 )
2020 ( 4 )
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 ( 22 )
2015 ( 15 )
2014 ( 25 )
2013 ( 4 )
2012 ( 10 )
2011 ( 7 )
2010 ( 11 )
2009 ( 3 )