Results 1 to 21 of 21

Thread: [RESOLVED] Fastest way to remove strings between two substrings?

Threaded View

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

    Re: [RESOLVED] Fastest way to remove strings between two substrings?

    The 10% increase comes purely from InStr vs. InStrB, LeftB$ and MidB$ are equal in speed to Left$ and Mid$

    The n test is only for returning a NULL string instead of EMPTY string when there is nothing to return. A very minor detail, personally prefer returning vbNullString over "".

    Edit!
    Checking for Mid$ copy length does matter. If there are empty items then speed increases. Here is an updated version of DeleteBetween.
    Code:
    Public Function DeleteBetween(ByVal sText As String, sBefore As String, sAfter As String) As String
        Dim P1 As Long, P2 As Long, L As Long, T As Long
        Dim L1 As Long: L1 = LenB(sBefore)
        Dim L2 As Long: L2 = LenB(sAfter)
        
        If LenB(sText) = 0 Or L1 = 0 Or L2 = 0 Then Exit Function
        P2 = 1 - L2
        Do
            P2 = P2 + L2: P1 = P2 - 1
            Do: P1 = InStrB(P1 + 1, sText, sBefore)
            Loop While (P1 And 1) = 0 And (P1 > 0)
            If P1 = 0 Then P1 = LenB(sText) + 1
            L = P1 - P2
            If L Then MidB$(sText, T + 1, L) = MidB$(sText, P2, L)
            T = T + L
            P2 = P1 + L1 - 1
            Do: P2 = InStrB(P2 + 1, sText, sAfter)
            Loop While (P2 And 1) = 0 And (P2 > 0)
        Loop While P2
        If T Then DeleteBetween = LeftB$(sText, T)
    End Function
    Updated attachment code includes the earlier test with some empty extra stuff. If you remove If L Then you'll see it slowing down –*testing the length is very quick.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Merri; May 12th, 2010 at 07:40 PM.

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