VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Extract String Function

by Peter B (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

This code will search a string for a given starting point(strFind) and a given end point(strSentinel) and return all the text in between. You can also add to the length of the start point if you don't want to include a number of unknown characters in the result

Rate Extract String Function

Function ExtractString(ByVal strText As String, _
               strFind As String, _
               intAddToLen As Integer, _
               strSentinel As String, _
               TrapErrors As Boolean, _
               intLength As Integer) As String
               
  Dim SStart     As Integer
  Dim SEnd      As Integer
   
  SStart = InStr(1, strText, strFind) + Len(strFind) + intAddToLen
  If SStart <= Len(strFind) And TrapErrors = True Then
    MsgBox """" & strFind & """ not found!", vbCritical, "Error"
    Exit Function
  End If
  
  SEnd = InStr(SStart, strText, strSentinel)
  If SEnd <= Len(strFind) And TrapErrors = True Then
    MsgBox "Sentinel value """ & strSentinel & """ not found!", vbCritical, "Error"
    Exit Function
  End If
  
  If intLength > 0 Then
    ExtractString = Mid(strText, SStart, intLength)
  Else
    ExtractString = Mid(strText, SStart, (SEnd - SStart))
  End If
End Function

Download this snippet    Add to My Saved Code

Extract String Function Comments

No comments have been posted about Extract String Function. Why not be the first to post a comment about Extract String Function.

Post your comment

Subject:
Message:
0/1000 characters