TagWrap() Function

This convenience function helps you write more readable code when building strings with HTML-style opening and closing tags.

TagWrap() Function

I use this convenience function to add opening and closing HTML-style tags to another string.  

I use the term "convenience function" to refer to simple functions that don't necessarily save you any lines of code, but they make your existing code easier to read.  

Html = "<b>" & FirstName & "</b>"

' vs.

Html = TagWrap(FirstName, "b")

There are a few benefits to this function:

  • Guarantees that the opening and closing tags match
  • Avoids typos, such as Html = "<b" & FirstName & "</b>"
  • Much easier to read when nested, e.g.:
    Html = TagWrap(TagWrap(FirstName, "b"), "i")

These benefits become clearer when you begin to incorporate the function into a more complex procedure.

Sample Usage

The Code

NOTE: The usage examples shown in the '>>> comments below are verifiable DocTests.

'>>> TagWrap("Mike", "b")
'<b>Mike</b>
'>>> TagWrap("Mike", "strong")
'<strong>Mike</strong>
Function TagWrap(TxtToWrap As String, Tag As String) As String
    TagWrap = "<" & Tag & ">" & TxtToWrap & "</" & Tag & ">"
End Function

Image by Alexas_Fotos from Pixabay

All original code samples by Mike Wolfe are licensed under CC BY 4.0