VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



GetTag, GetTagText, CutTag

by Jamie Richard Wilson (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

This is a set of three functions that pull tagged data, such as that from HTML or XML, based on the tag name. I've used this in a number of applications where I've need to store multiple bits of variable-length data in a single string or file.

Inputs
A string containing tags which in turn contain data you are searching for AND the name of the tag. For instance, if you're pulling from HTML for anything between

and

then you would enter "H1" as the tag name.
Code Returns
GetTag() returns the data between the specified tags along with the tags. GetTagText() returns the data between the specified tags, without the tags. CutTag() returns the original text minus the specified tags and the data between them.
Rate GetTag, GetTagText, CutTag

Public Function GetTag(SourceString As String, Tag As String) As String
  'Gets the tag and text between it
  If InStr(SourceString, "<" & Tag & ">") = 0 Then
    GetTag = ""
    Exit Function
  End If
  GetTag = Mid$(SourceString, InStr(SourceString, "<" & Tag & ">"), InStr(SourceString, "") + Len("") - 1)
End Function
Public Function GetTagText(SourceString As String, Tag As String) As String
  'Grabs the text between tags
  If InStr(SourceString, "<" & Tag & ">") = 0 Then
    GetTagText = ""
    Exit Function
  End If
  GetTagText = Mid$(SourceString, InStr(SourceString, "<" & Tag & ">") + Len("<" & Tag & ">"), (InStr(SourceString, "")) - (InStr(SourceString, "<" & Tag & ">") + Len("<" & Tag & ">")))
End Function
 Public Function CutTag(SourceString As String, Tag As String) As String
  'Cuts the entire tag out of the text
  If InStr(SourceString, "<" & Tag & ">") = 0 Then
    CutTag = ""
    Exit Function
  End If
  CutTag = Left$(SourceString, InStr(SourceString, "<" & Tag & ">") - 1) & Mid$(SourceString, InStrRev(SourceString, "") + Len(""))
End Function

Download this snippet    Add to My Saved Code

GetTag, GetTagText, CutTag Comments

No comments have been posted about GetTag, GetTagText, CutTag. Why not be the first to post a comment about GetTag, GetTagText, CutTag.

Post your comment

Subject:
Message:
0/1000 characters